我能够通过完全删除路由并将 Url 属性和 Title 属性添加到 BrowserPage 的代码隐藏来解决这个问题:
private string url;
public string Url {
get => url;
set {
url = value;
((BrowserViewModel)BindingContext).SetSource(url);
}
}
private string pageTitle;
public string PageTitle {
get => pageTitle;
set {
pageTitle = value;
((BrowserViewModel)BindingContext).SetPageTitle(pageTitle);
}
}
在 AppShell.xaml 文件中,我删除了Route
and ContentTemplate
,并ShellContent.Content
按照此处所述设置了。这允许我设置 url 和标题:
<ShellContent Title="Page 1" Icon="icon_about.png" >
<local:BrowserPage Url="http://www.yahoo.com" PageTitle="Page 1"/>
</ShellContent>
<ShellContent Title="Page 2" Icon="icon_about.png">
<local:BrowserPage Url="http://www.google.com" PageTitle="Page 2"/>
</ShellContent>
当应用程序启动并设置 Url 和 PageTitle 时,它会将其传递给 ViewModel 并加载页面。
任何需要实现这一目标的人的完整代码。
浏览器页面.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:vm="clr-namespace:MyApp.ViewModels"
x:Class="MyApp.Views.BrowserPage"
Title="{Binding Title}">
<ContentPage.BindingContext>
<vm:BrowserViewModel/>
</ContentPage.BindingContext>
<ContentPage.Resources>
<ResourceDictionary>
<Color x:Key="Accent">#96d1ff</Color>
</ResourceDictionary>
</ContentPage.Resources>
<Grid>
<WebView
x:Name="mywebview"
Source="{Binding Source}"
Cookies="{Binding Cookies}"
Navigating="mywebview_Navigating"
Navigated="mywebview_Navigated"
WidthRequest="1000"
HeightRequest="1000"/>
<ActivityIndicator
x:Name="spinner"
Color="Black"
IsRunning="True"
HorizontalOptions="Center"
VerticalOptions="Center"/>
</Grid>
</ContentPage>
浏览器页面.xaml.cs
using MyApp.ViewModels;
using System.Diagnostics;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace MyApp.Views {
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class BrowserPage : ContentPage {
private string url;
public string Url {
get => url;
set {
url = value;
((BrowserViewModel)BindingContext).SetSource(url);
}
}
private string pageTitle;
public string PageTitle {
get => pageTitle;
set {
pageTitle = value;
((BrowserViewModel)BindingContext).SetPageTitle(pageTitle);
}
}
public BrowserPage() {
InitializeComponent();
}
private void mywebview_Navigated(object sender, WebNavigatedEventArgs e) {
mywebview.IsVisible = true;
spinner.IsVisible = false;
}
private void mywebview_Navigating(object sender, WebNavigatingEventArgs e) {
mywebview.IsVisible = false;
spinner.IsVisible = true;
}
}
}
浏览器视图模型.cs
using System;
using System.Net;
using Xamarin.Forms;
namespace MyApp.ViewModels {
public class BrowserViewModel : BaseViewModel {
private string text;
public string Text {
get => text;
set {
SetProperty(ref text, value);
}
}
private UrlWebViewSource source;
public UrlWebViewSource Source {
get {
return source;
}
set {
SetProperty(ref source, value);
}
}
private CookieContainer cookies;
public CookieContainer Cookies {
get {
return cookies;
}
set {
SetProperty(ref cookies, value);
}
}
public BrowserViewModel() { }
public BrowserViewModel(string title) {
Title = title;
}
public void SetPageTitle(string title) {
Title = title;
}
public void SetSource(string Url) {
CookieContainer cookieJar = new CookieContainer();
Uri uri = new Uri(Url, UriKind.RelativeOrAbsolute);
Cookie cookie = new Cookie {
...
};
cookieJar.Add(uri, cookie);
Cookies = cookieJar;
Source = new UrlWebViewSource { Url = uri.ToString() };
}
}
}
AppShell.xaml
<?xml version="1.0" encoding="UTF-8"?>
<Shell xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:MyApp.Views"
Title="MyApp"
x:Class="MyApp.AppShell">
<Shell.Resources>
<ResourceDictionary>
<Style x:Key="BaseStyle" TargetType="Element">
<Setter Property="Shell.BackgroundColor" Value="{StaticResource Primary}" />
<Setter Property="Shell.ForegroundColor" Value="White" />
<Setter Property="Shell.TitleColor" Value="White" />
<Setter Property="Shell.DisabledColor" Value="#B4FFFFFF" />
<Setter Property="Shell.UnselectedColor" Value="#95FFFFFF" />
<Setter Property="Shell.TabBarBackgroundColor" Value="{StaticResource Primary}" />
<Setter Property="Shell.TabBarForegroundColor" Value="White"/>
<Setter Property="Shell.TabBarUnselectedColor" Value="#95FFFFFF"/>
<Setter Property="Shell.TabBarTitleColor" Value="White"/>
</Style>
<Style TargetType="TabBar" BasedOn="{StaticResource BaseStyle}" />
<Style TargetType="FlyoutItem" BasedOn="{StaticResource BaseStyle}" />
</ResourceDictionary>
</Shell.Resources>
<TabBar>
<ShellContent Title="Page 1" Icon="icon_about.png" >
<local:BrowserPage Url="http://www.yahoo.com" PageTitle="Page 1"/>
</ShellContent>
<ShellContent Title="Page 2" Icon="icon_about.png">
<local:BrowserPage Url="http://www.google.com" PageTitle="Page 2"/>
</ShellContent>
</TabBar>
</Shell>