Xamarin.Forms 使用 ViewModel 导航到另一个页面
Xamarin.Forms Navigate to another page using ViewModel
我有几个 ContentPages,我想通过单击页面中的元素从一个导航到另一个。我有我的 ViewModel class:
class JumpVM : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private INavigation _navigation;
public ICommand NewPage
{
get
{
return new Command(async () =>
{
await _navigation.PushAsync(new MySettingsPage());
});
}
}
public JumpVM() { }
public JumpVM(INavigation navitation)
{
_navigation = navitation;
}
}
这是我的其中一页(为了space,我只放了相关代码):
BindingContext = new JumpVM(this.Navigation);
.....
Image fbInvite = new Image
{
Source = ImageSource.FromResource(Constants.ASSETLOCATION + ".facebookInviteIcon.png"),
HorizontalOptions = LayoutOptions.Center
};
fbInvite.GestureRecognizers.Add(new TapGestureRecognizer(sender =>
{
//navigation in the method below
FaceboonInviteFriends();
fbInvite.Opacity = 0.8;
fbInvite.FadeTo(1);
}));
我想在单击图像时在 JumpVM class 中执行命令并导航到那里的页面。我该怎么做?
尝试在 FadeTo
行之后添加以下行:
((JumpVM)BindingContext).NewPage.Execute(null)
.
这是在 ViewModel 概念中将一页导航到另一页的答案。
public ICommand NavigationList { get; set; }
NavigationList = new Command(GetListview);
public void GetListview()
{
Xamarin.Forms.Application.Current.MainPage.Navigation.PushAsync(new ListViewPerson());
}
如果您使用的是 ViewModel,则可以使用 ICommand 轻松实现。
namespace YourApp.ViewModels
{
public class CurrentPageViewModel
{
public ICommand BackToPage {get; private set; }
public CurrentPageViewModel()
{
BackToPage = new Command(async () => {
await Application.Current.MainPage.Navigation.PushModalAsync(new MainPage());
});
}
}
}
而在你想去的页面的ViewModel中,你需要实现如下的PopAsync。
namespace YourApp.ViewModels
{
public class MainPageViewModel
{
public ICommand BackToMain { get; private set; }
public MainPageViewModel()
{
BackToMain = new Command(async () => {
await Application.Current.MainPage.Navigation.PopAsync();
});
}
}
}
还请记住在当前页面和您想要的 Views CodeBehind 中使用绑定。
namespace RealmApp1.Views
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
BindingContext = new MainPageViewModel();
}
}
}
希望对你有用!
有一个好的代码!!!
我有几个 ContentPages,我想通过单击页面中的元素从一个导航到另一个。我有我的 ViewModel class:
class JumpVM : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private INavigation _navigation;
public ICommand NewPage
{
get
{
return new Command(async () =>
{
await _navigation.PushAsync(new MySettingsPage());
});
}
}
public JumpVM() { }
public JumpVM(INavigation navitation)
{
_navigation = navitation;
}
}
这是我的其中一页(为了space,我只放了相关代码):
BindingContext = new JumpVM(this.Navigation);
.....
Image fbInvite = new Image
{
Source = ImageSource.FromResource(Constants.ASSETLOCATION + ".facebookInviteIcon.png"),
HorizontalOptions = LayoutOptions.Center
};
fbInvite.GestureRecognizers.Add(new TapGestureRecognizer(sender =>
{
//navigation in the method below
FaceboonInviteFriends();
fbInvite.Opacity = 0.8;
fbInvite.FadeTo(1);
}));
我想在单击图像时在 JumpVM class 中执行命令并导航到那里的页面。我该怎么做?
尝试在 FadeTo
行之后添加以下行:
((JumpVM)BindingContext).NewPage.Execute(null)
.
这是在 ViewModel 概念中将一页导航到另一页的答案。
public ICommand NavigationList { get; set; }
NavigationList = new Command(GetListview);
public void GetListview()
{
Xamarin.Forms.Application.Current.MainPage.Navigation.PushAsync(new ListViewPerson());
}
如果您使用的是 ViewModel,则可以使用 ICommand 轻松实现。
namespace YourApp.ViewModels
{
public class CurrentPageViewModel
{
public ICommand BackToPage {get; private set; }
public CurrentPageViewModel()
{
BackToPage = new Command(async () => {
await Application.Current.MainPage.Navigation.PushModalAsync(new MainPage());
});
}
}
}
而在你想去的页面的ViewModel中,你需要实现如下的PopAsync。
namespace YourApp.ViewModels
{
public class MainPageViewModel
{
public ICommand BackToMain { get; private set; }
public MainPageViewModel()
{
BackToMain = new Command(async () => {
await Application.Current.MainPage.Navigation.PopAsync();
});
}
}
}
还请记住在当前页面和您想要的 Views CodeBehind 中使用绑定。
namespace RealmApp1.Views
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
BindingContext = new MainPageViewModel();
}
}
}
希望对你有用! 有一个好的代码!!!