Wal*_*had 10 c# xaml cross-platform mvvm xamarin.forms
我正在开发xamarin.form跨平台应用程序,我希望在按钮点击时从一个页面导航到另一个页面.正如我Navigation.PushAsync(new Page2());
在ViewModel中所做的那样,因为它只能在Code-Behid文件中使用.请建议任何方式这样做?
这是我的观点:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Calculator.Views.SignIn"
xmlns:ViewModels="clr-namespace:Calculator.ViewModels;assembly=Calculator">
<ContentPage.BindingContext>
<ViewModels:LocalAccountViewModel/>
</ContentPage.BindingContext>
<ContentPage.Content>
<StackLayout>
<Button Command="{Binding ContinueBtnClicked}"></Button>
</StackLayout>
</ContentPage.Content>
</ContentPage>
Run Code Online (Sandbox Code Playgroud)
这是我的ViewModel:
public class LocalAccountViewModel : INotifyPropertyChanged
{
public LocalAccountViewModel()
{
this.ContinueBtnClicked = new Command(GotoPage2);
}
public void GotoPage2()
{
/////
}
public ICommand ContinueBtnClicked
{
protected set;
get;
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanges([CallerMemberName] string PropertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(PropertyName));
}
}
Run Code Online (Sandbox Code Playgroud)
Tai*_*T's 20
一种方法是您可以通过VM构造函数传递导航.由于页面继承自VisualElement
,因此它们直接继承该Navigation
属性.
代码隐藏文件:
public class SignIn : ContentPage
{
public SignIn(){
InitializeComponent();
// Note the VM constructor takes now a INavigation parameter
BindingContext = new LocalAccountViewModel(Navigation);
}
}
Run Code Online (Sandbox Code Playgroud)
然后在您的VM中,添加一个INavigation
属性并更改构造函数以接受a INavigation
.然后,您可以使用此属性进行导航:
public class LocalAccountViewModel : INotifyPropertyChanged
{
public INavigation Navigation { get; set;}
public LocalAccountViewModel(INavigation navigation)
{
this.Navigation = navigation;
this.ContinueBtnClicked = new Command(async () => await GotoPage2());
}
public async Task GotoPage2()
{
/////
await Navigation.PushAsync(new Page2());
}
...
Run Code Online (Sandbox Code Playgroud)
请注意您应修复的代码问题:GoToPage2()
必须设置该方法async
并返回Task
类型.此外,该命令将执行异步操作调用.这是因为你必须异步进行页面导航!
希望能帮助到你!
Ale*_*aro 14
一个简单的方法是
this.ContinueBtnClicked = new Command(async()=>{
await Application.Current.MainPage.Navigation.PushAsync(new Page2());
});
Run Code Online (Sandbox Code Playgroud)