c# – 使用MvvmCross添加Windows 8.1存储基本页面(不再继承自不存在的LayoutAware但基本页面类)
作者:互联网
在Windows 8.1中,当您添加基于Windows 8.1 Basic Page的View / Page时,它不再继承LayoutAware类,因为它在Win 8.1中不再存在.现在,所有基本页面都直接从Page类继承.此外,它不再具有OnNavigatedTo / OnNavigatedFrom事件,因为Win8.1 Basic页面现在利用NavigationHelper类并调用this.navigationHelper.LoadState和this.navigationHelper.SaveState事件处理程序.如果使用TipCalc示例并添加Windows Store基本页面,TipView,初始页面将如下所示:
public sealed partial class TipView : Page
{
private NavigationHelper navigationHelper;
private ObservableDictionary defaultViewModel = new ObservableDictionary();
/// <summary>
/// This can be changed to a strongly typed view model.
/// </summary>
public ObservableDictionary DefaultViewModel
{
get { return this.defaultViewModel; }
}
/// <summary>
/// NavigationHelper is used on each page to aid in navigation and
/// process lifetime management
/// </summary>
public NavigationHelper NavigationHelper
{
get { return this.navigationHelper; }
}
public TipView()
{
this.InitializeComponent();
this.navigationHelper = new NavigationHelper(this);
this.navigationHelper.LoadState += navigationHelper_LoadState;
this.navigationHelper.SaveState += navigationHelper_SaveState;
}
由于TipView页面现在直接从Page继承,如果您将TipView页面更改为继承自MvxStorePage,如下所示:
public sealed partial class TipView : MvxStorePage
{
private NavigationHelper navigationHelper;
private ObservableDictionary defaultViewModel = new ObservableDictionary();
由于Page是部分类,因此会发生以下错误:
Partial declarations of ‘TipCalc.CrossPlat.WinStore.Views.TipView’ must not specify different base classes.
即使它允许将基类更改为MvxStorePage,也不能在LoadState事件处理程序中添加base.OnNavigatedTo(e),如下所示:
private void navigationHelper_LoadState(object sender, LoadStateEventArgs e)
{
base.OnNavigatedTo(e);
}
因为OnNavigatedTo中的e参数正在寻找NavigationEventArgs与LoadStateEventArgs.
任何帮助将不胜感激,因为我需要完成具有Windows 8.1实现的跨平台PCL项目.
解决方法:
我找到了如何在没有我在下面提到的黑客攻击的情况下解决这个问题的答案,但是黑客引导我找到答案.我发布的其他任何可能有问题的人想要添加一个继承自MvvmCross商店页面的Win8.1页面.
答案在于,在Page.xaml.cs中找到的Win8 / Win8.1 Page类是一个部分类,它在CS和XAML中构建/包含整个完成的类定义,它们被共同编译以构建整个类对于页面.
因此,如果您有TipView页面,请通过更改代码更改从MvxStorePage继承的TipView页面,如下所示:
public sealed partial class TipView: MvxStorePage
然后转到TipView页面XAML文件并更改默认根页面节点的声明:
<Page
x:Name="pageRoot"
x:Class="TipCalc.CrossPlat.WinStore.Views.TipView"
DataContext="{Binding DefaultViewModel, RelativeSource={RelativeSource Self}}"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:TipCalc.CrossPlat.WinStore.Views"
xmlns:common="using:TipCalc.CrossPlat.WinStore.Common"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
要更改为以下页面节点声明并添加MvvmCross MvxStorePage命名空间,如下所示:
<StorePage:MvxStorePage
x:Name="pageRoot"
x:Class="TipCalc.CrossPlat.WinStore.Views.TipView"
DataContext="{Binding DefaultViewModel, RelativeSource={RelativeSource Self}}"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:TipCalc.CrossPlat.WinStore.Views"
xmlns:StorePage="using:Cirrious.MvvmCross.WindowsStore.Views"
xmlns:common="using:TipCalc.CrossPlat.WinStore.Common"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
需要注意的关键点:StorePage中的StorePage命名空间:MvxStorePage是通过将命名空间添加到Cirrious.MvvmCross.WindowsStore.Views来引用的:
xmlns:StorePage="using:Cirrious.MvvmCross.WindowsStore.Views"
在XAML页面声明中.一旦我完成了这个,它就像一个魅力.
标签:c,xamarin,windows-store-apps,mvvmcross,windows-8-1 来源: https://codeday.me/bug/20190529/1175706.html