FreshMVVM - 从不继承自 FreshBasePageModel 的子 ContentView 打开页面的最佳方式
FreshMVVM - best way to open a page from a child ContentView that doesn't inherit from FreshBasePageModel
以下代码显示了 OpenPage Command
的 2 个示例。 MainPageModel
中的那个有效,因为它直接派生自 FreshBasePageModel
。但是,ChildPageModel
中的第二个 OpenPage 调用将不起作用(或编译)。我不想到处传递父模型。那么,如何使用 FreshMVVM 从 ChildPageModel
打开一个新页面(并使后退按钮起作用等)?
public class MainPageModel : FreshBasePageModel
{
public Command OpenPage
{
get
{
return new Command(() =>
{
CoreMethods.PushPageModel<NewPageModel>();
});
}
}
public ChildPageModel ChildPageModel { get; set; }
}
public class ChildPageModel
{
public Command OpenPage
{
get
{
return new Command(() =>
{
// ??????
CoreMethods.PushPageModel<NewPageModel>();
});
}
}
}
您还应该使 ChildPageModel 继承自 FreshBasePageModel。所有 PageModel 都应继承自 FreshBasePageModel
我用三个页面(MainPage、SecondPage、ThirdPage)做了一个简单的例子。您可以从 HitHub 下载 FreshMVVMDemo 文件夹的源文件。
https://github.com/WendyZang/Test.git
如果要打开新页面,可以在子页面添加命令。
#region Commands
public Command GotoPageCommand
{
get
{
return new Command(async () =>
{
await CoreMethods.PushPageModel<ThirdPageModel>(); //replace the ThirdPageModel with the page you want to open
});
}
}
#endregion
如果要返回,请添加如下命令。
#region Commands
public Command GoBackSecondCommand
{
get
{
return new Command(async () =>
{
//await CoreMethods.PopPageModel(); //go back to main page
await CoreMethods.PushPageModel<SecondPageModel>(); //Go back to third page
});
}
}
#endregion
下面的代码将完成这个...
var page = FreshPageModelResolver.ResolvePageModel<MainPageModel>();
var model = page.GetModel() as MainPageModel;
var navService = FreshMvvm.FreshIOC.Container.Resolve<IFreshNavigationService>();
await navService.PushPage(page, null);
以下代码显示了 OpenPage Command
的 2 个示例。 MainPageModel
中的那个有效,因为它直接派生自 FreshBasePageModel
。但是,ChildPageModel
中的第二个 OpenPage 调用将不起作用(或编译)。我不想到处传递父模型。那么,如何使用 FreshMVVM 从 ChildPageModel
打开一个新页面(并使后退按钮起作用等)?
public class MainPageModel : FreshBasePageModel
{
public Command OpenPage
{
get
{
return new Command(() =>
{
CoreMethods.PushPageModel<NewPageModel>();
});
}
}
public ChildPageModel ChildPageModel { get; set; }
}
public class ChildPageModel
{
public Command OpenPage
{
get
{
return new Command(() =>
{
// ??????
CoreMethods.PushPageModel<NewPageModel>();
});
}
}
}
您还应该使 ChildPageModel 继承自 FreshBasePageModel。所有 PageModel 都应继承自 FreshBasePageModel
我用三个页面(MainPage、SecondPage、ThirdPage)做了一个简单的例子。您可以从 HitHub 下载 FreshMVVMDemo 文件夹的源文件。 https://github.com/WendyZang/Test.git
如果要打开新页面,可以在子页面添加命令。
#region Commands
public Command GotoPageCommand
{
get
{
return new Command(async () =>
{
await CoreMethods.PushPageModel<ThirdPageModel>(); //replace the ThirdPageModel with the page you want to open
});
}
}
#endregion
如果要返回,请添加如下命令。
#region Commands
public Command GoBackSecondCommand
{
get
{
return new Command(async () =>
{
//await CoreMethods.PopPageModel(); //go back to main page
await CoreMethods.PushPageModel<SecondPageModel>(); //Go back to third page
});
}
}
#endregion
下面的代码将完成这个...
var page = FreshPageModelResolver.ResolvePageModel<MainPageModel>();
var model = page.GetModel() as MainPageModel;
var navService = FreshMvvm.FreshIOC.Container.Resolve<IFreshNavigationService>();
await navService.PushPage(page, null);