我是Windows手机上的新手。我想创建一个应用程序但实际上我遇到了Binding命令的问题。
我想点击,更改视图
在我的主页上我有2个按钮:在我的MainPage.xaml上的View文件夹中登录/注册
<phone:PhoneApplicationPage>
<Grid>
<Grid.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Black" Offset="0"/>
<GradientStop Color="#FF563AA0" Offset="1"/>
</LinearGradientBrush>
</Grid.Background>
<ContentControl Content="{Binding CurrentViewModel}" />
<Button
Command="{Binding ConnexionViewCommand}"
x:Name="Connexion_button"
HorizontalAlignment="Left"
Margin="90,308,0,0"
Content="Login"
VerticalAlignment="Top"
Width="282"/>
<Button
Command="{Binding InscriptionViewCommand}"
x:Name="Create_account"
HorizontalAlignment="Left"
Margin="90,446,0,0"
Content="Create Account"
VerticalAlignment="Top"
Width="282"/>
</Grid>
</phone:PhoneApplicationPage>
我的文件夹中有3个文件查看:ConnexionView.xaml和InscriptionView.xaml,以及MainPage.xaml
我的文件夹ViewModel中的5个文件:ConnexionViewModel.cs,InscriptionViewModel.cs,MainPageViewModel.cs,MainViewModel.cs和ViewModelLocator.cs
我尝试按照这个例子,但我不太了解。
http://www.codeproject.com/Articles/323187/MVVMLight-Using-Two-Views
我在MainViewModel.cs中添加了这段代码,但我不知道我现在要做什么。
namespace TeysserApp.ViewModel
{
public class MainViewModel : ViewModelBase
{
private ViewModelBase _currentViewModel;
readonly static ConnexionViewModel _ConnexionViewModel = new ConnexionViewModel();
readonly static InscriptionViewModel _InscriptionViewModel = new InscriptionViewModel();
readonly static MainPageViewModel _MainPageViewModel = new MainPageViewModel();
public ViewModelBase CurrentViewModel
{
get
{
return _currentViewModel;
}
set
{
if (_currentViewModel == value)
return;
_currentViewModel = value;
RaisePropertyChanged("CurrentViewModel");
}
}
public ICommand ConnexionViewCommand { get; private set; }
public ICommand InscriptionViewCommand { get; private set; }
public MainViewModel()
{
CurrentViewModel = MainViewModel._MainPageViewModel;
ConnexionViewCommand = new RelayCommand(() => ExecuteCommandViewCommand());
InscriptionViewCommand = new RelayCommand(() => ExecuteInscriptionViewCommand());
}
private void ExecuteConnexionViewCommand()
{
CurrentViewModel = MainViewModel._ConnexionViewModel;
}
private void ExecuteSecondViewCommand()
{
CurrentViewModel = MainViewModel._InscriptionViewModel;
}
}
}
这是我的ViewModelLocator.cs
namespace TestApp.ViewModel
{
/// <summary>
/// This class contains static references to all the view models in the
/// application and provides an entry point for the bindings.
/// <para>
/// See http://www.galasoft.ch/mvvm
/// </para>
/// </summary>
public class ViewModelLocator
{
static ViewModelLocator()
{
ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
if (ViewModelBase.IsInDesignModeStatic)
{
// SimpleIoc.Default.Register<IDataService, Design.DesignDataService>();
}
else
{
// SimpleIoc.Default.Register<IDataService, DataService>();
}
SimpleIoc.Default.Register<MainViewModel>();
SimpleIoc.Default.Register<ConnexionViewModel>();
SimpleIoc.Default.Register<InscriptionViewModel>();
}
/// <summary>
/// Gets the Main property.
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
"CA1822:MarkMembersAsStatic",
Justification = "This non-static member is needed for data binding purposes.")]
public MainViewModel Main
{
get { return ServiceLocator.Current.GetInstance<MainViewModel>(); }
}
public ConnexionViewModel ConnexionView
{
get { return ServiceLocator.Current.GetInstance<ConnexionViewModel>(); }
}
public InscriptionViewModel InscriptionView
{
get { return ServiceLocator.Current.GetInstance<InscriptionViewModel>(); }
}
}
}
这是我的App.xaml
<Application
x:Class="TeysserApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:views="clr-namespace:TeysserApp.Views"
xmlns:vm="clr-namespace:TeysserApp.ViewModels"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone">
<Application.Resources>
<vm:ViewModelLocator x:Key="Locator" />
<ResourceDictionary>
<DataTemplate DataType="{x:Type vm:ConnexionViewModel}">
<view:ConnexionView />
</DataTemplate>
<DataTemplate DataType="{x:Type vm:InscriptionViewModel}">
<view:InscriptionView />
</DataTemplate>
</ResourceDictionary>
<local:LocalizedStrings xmlns:local="clr-namespace:TeysserApp" x:Key="LocalizedStrings"/>
</Application.Resources>
<Application.ApplicationLifetimeObjects>
<!--Objet requis qui gère les événements de durée de vie pour l'application-->
<shell:PhoneApplicationService
Launching="Application_Launching" Closing="Application_Closing"
Activated="Application_Activated" Deactivated="Application_Deactivated"/>
</Application.ApplicationLifetimeObjects>
</Application>
感谢您的帮助。
答案 0 :(得分:2)
在MVVM Light 5 Laurent介绍了NavigationService
public ViewModelLocator()
{
ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
var navigationService = this.CreateNavigationService();
SimpleIoc.Default.Register<INavigationService>(() => navigationService);
SimpleIoc.Default.Register<IDialogService, DialogService>();
SimpleIoc.Default.Register<MainViewModel>();
SimpleIoc.Default.Register<DetailsViewModel>();
}
private INavigationService CreateNavigationService()
{
var navigationService = new NavigationService();
navigationService.Configure("Details", typeof(DetailsPage));
// navigationService.Configure("key1", typeof(OtherPage1));
//From a working project.
navigationService.Configure("tnc", new System.Uri("/Views/TncAgreement.xaml", System.UriKind.Relative));
return navigationService;
}
您的ViewModel
public class MainViewModel : ViewModelBase
{
private INavigationService navigationService;
public RelayCommand DetailsCommand { get; set; }
public MainViewModel(INavigationService navigationService)
{
this.navigationService = navigationService;
//Show Terms and condition agreement;
navigationService.NavigateTo("tnc");
DetailsCommand = new RelayCommand(() =>
{
navigationService.NavigateTo("Details", "My data");
});
}
}
类似SO Answer
答案 1 :(得分:0)