我是Silverlight的新手,并且一直在使用Simple MVVM来创建学习应用程序。 Simple MVVM作者提供了几个列表/详细数据绑定的好例子,但我正在查看注册用户的屏幕示例。由于这是一个内部应用程序,我可以从托管网站传入Windows ID并根据用户数据库进行检查。如果用户ID不存在,我想显示一个注册,用户可以在其中输入他们的名/姓,然后单击“注册”按钮以保存新的用户记录。看起来像一个简单的模式,但我无法弄明白。
答案 0 :(得分:0)
我设法结合了几个不同的例子,并提出了一个有效的例子。这是我正在创建的学习应用程序的一部分。它允许用户(在我的IT部门)建议并投票选择午餐目的地。对于这个应用程序,用餐者是用户。除了“注册”按钮之外,下面的代码将显示一个屏幕,其中包含用户的Windows ID(只读)以及名/姓的输入。
XAML(registerdinerview.xaml):
<navigation:Page x:Class="LTDestination.Views.RegisterDinerView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
d:DesignWidth="640"
d:DesignHeight="480"
Title="RegisterDinerView Page"
DataContext="{Binding Source={StaticResource Locator}, Path=RegisterDinerViewModel}"
>
<navigation:Page.Resources>
<Style TargetType="Button">
<Style.Setters>
<Setter Property="Height" Value="23"/>
<Setter Property="Width" Value="60"/>
<Setter Property="Margin" Value="5,0,0,0"/>
</Style.Setters>
</Style>
</navigation:Page.Resources>
<StackPanel x:Name="LayoutRoot">
<StackPanel x:Name="ContentStackPanel">
<TextBlock x:Name="HeaderText" Style="{StaticResource HeaderTextStyle}" Text="New User Registration"/>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<sdk:Label Content="User Id:" Grid.Row="0" />
<TextBlock Grid.Row="0" Grid.Column="1" Height="30" Text="{Binding Path=Model.UserId}" />
<sdk:Label Content="First Name:" Grid.Row="1" />
<TextBox Grid.Row="1" Grid.Column="1" Height="30" Text="{Binding Path=Model.FirstName, Mode=TwoWay}" />
<sdk:Label Content="Last Name:" Grid.Row="2" />
<TextBox Grid.Row="2" Grid.Column="1" Height="30" Text="{Binding Path=Model.LastName, Mode=TwoWay}" />
<Button x:Name="RegisterButton" Content="Register" Click="registerButton_Click"
Grid.Row="3" Grid.Column="1" Height="23" Width="75" HorizontalAlignment="Left" Margin="5,0,0,0"/>
</Grid>
</StackPanel>
</StackPanel>
查看代码(registerdinerview.xaml.cs):
namespace LTDestination.Views
{
public partial class RegisterDinerView : Page
{
RegisterDinerViewModel _ViewModel;
public RegisterDinerView()
{
InitializeComponent();
_ViewModel = (RegisterDinerViewModel)DataContext;
_ViewModel.NewDiner();
}
private void registerButton_Click(object sender, RoutedEventArgs e)
{
_ViewModel.SaveChanges();
}
// Executes when the user navigates to this page.
protected override void OnNavigatedTo(NavigationEventArgs e)
{
}
}
}
<强>视图模型:强>
using System;
使用System.Collections.Generic;
// Toolkit命名空间 使用LTDestination.Services; 使用LTDestination.Web; 使用SimpleMvvmToolkit;
命名空间LTDestination.ViewModels { /// ///此类扩展了实现IEditableDataObject的ViewModelDetailBase。 /// ///指定正在编辑的类型 DetailType 作为第二个类型参数 ///并作为第二个ctor的参数。 /// /// ///使用 mvvmprop 代码段向此ViewModel添加可绑定属性。 /// /// public class RegisterDinerViewModel:ViewModelDetailBase { #region初始化和清理
private IDinerServiceAgent serviceAgent;
public RegisterDinerViewModel()
{
}
public RegisterDinerViewModel(IDinerServiceAgent serviceAgent)
{
this.serviceAgent = serviceAgent;
}
#endregion
#region Notifications
public event EventHandler<NotificationEventArgs<Exception>> ErrorNotice;
#endregion
#region Properties
private bool _IsBusy;
public bool IsBusy
{
get { return _IsBusy; }
set
{
_IsBusy = value;
NotifyPropertyChanged(m => m.IsBusy);
}
}
#endregion
#region Methods
public void NewDiner()
{
serviceAgent.GetDiners(DinersLoaded);
}
public void SaveChanges()
{
serviceAgent.AddDiner(base.Model);
serviceAgent.SaveChanges(DinerSaved);
IsBusy = true;
}
#endregion
#region Completion Callbacks
private void DinersLoaded(List<Diner> entities, Exception error)
{
// If no error is returned, set the model to entities
if (error == null)
{
base.Model = new Diner { UserId = App.UserId, Active = true };
}
// Otherwise notify view that there's an error
else
{
NotifyError("Unable to retrieve items", error);
}
// We're done
IsBusy = false;
}
private void DinerSaved(Exception error)
{
if (error != null)
{
NotifyError("Unable to save diner", error);
}
else
{
MessageBus.Default.Notify(MessageTokens.Navigation, this, new NotificationEventArgs(PageNames.Home));
}
}
#endregion
#region Helpers
// Helper method to notify View of an error
private void NotifyError(string message, Exception error)
{
// Notify view of an error
Notify(ErrorNotice, new NotificationEventArgs<Exception>(message, error));
}
#endregion
}
}