如何为 windows phone 8 消息传递应用程序绑定 ViewModel
How to bind the ViewModel for a windows phone 8 messaging app
My model:
public class MyMessageModel
{
public string DisplaySender { get; set; }
//how does the below observable collection needs to be changed ,
//if I want to add another field to itemssource template.
//e.g. public DateTime Timestamp { get; set; }
public ObservableCollection<string> MessagesExchanged { get; set; }
public string NewMessage { get; set; }
}
Chat.xaml:
<TextBlock Name="lblFromUserName" Text="{Binding DisplaySender ,Mode=TwoWay}" Height="65" Style="{StaticResource PhoneTextNormalStyle}" FontSize="35"/>
<ItemsControl ItemsSource="{Binding Path=MessagesExchanged}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding ????,Mode=TwoWay}" />
<TextBlock Text="{Binding Path=Timestamp}" HorizontalAlignment="Right" VerticalAlignment="Bottom" Grid.Row="1"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl
<StackPanel Orientation="Horizontal" Grid.Row="1">
<TextBox Grid.Column="0" Name="txtNewMessage" Text="{Binding NewMessage,Mode=TwoWay}" Margin="0,0,0,0" Width="350"/>
<Button Grid.Column="1" Command="{Binding SendClickCommand,Mode=TwoWay}" Name="btnSend" Content="Send" Width="100" />
</StackPanel>
Chat.xaml.cs 如下所示:
public class Chat: PhoneApplicationPage
{
private MyMessageViewModel _MyMessageViewModel;
public Conversation()
{
InitializeComponent();
_MyMessageViewModel = new MyMessageViewModel();
this.DataContext = _MyMessageViewModel;
}
}
我的 ViewModel MyMessageViewModel 如下所示:
public System.Windows.Input.ICommand SendClickCommand
{
get
{
return new DelegateCommand((o) =>
{
Task.Factory.StartNew(() =>
{
//loop through the selected items and clear everything
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
try
{
//DO YOUR WORK HERE: TAKE THE NEW MESSAGE AND APPEND IT TO THE MESSAGES EXCHANGED
}
catch (Exception)
{
throw;
}
});
});
});
}
}
现在,当用户在上面的名为 Chat.xaml 的视图中(用户将从主页来到此页面)时,我想用顶部的 DisplaySender 值加载它,该值将在整个 conversation.Value 将此字段作为导航参数从主页传递。
并且每次用户单击“发送”按钮时,在 SendClickCommand 中仅通过从 txtNewMessage 字段添加新消息来更新 MessagesExchange 集合,然后清除该字段。
我这里有两个问题:
当用户第一次访问 Chat.xaml 时,我如何绑定三个字段的数据,例如DisplaySender(非空值将作为导航参数传递),MessagesExchange(最初在启动新对话时将为空,否则它将具有来自导航参数的非空值)和 NewMessage(最初这将始终为空)。
其次在 SendClickCommand 中通知 属性 我如何从 txtNewMessage 中获取文本并更新 ObservableCollection MessagesExchange 并在最后清除 txtNewMessage 的值。以及如何将 MessagesExchange 的值绑定到数据模板文本块字段?
我猜你试图在从主页导航到聊天页面时传递 Class MyMessageModel 的对象。
所以定义一个属性
private MyMessageModel currentMessageModel;
public MyMessageModel CurrentMessageModel
{
get { return currentMessageModel; }
set { currentMessageModel = value; }
}
并在 ChatPage 的 OnNavigatedTo 方法中设置
CurrentMessageModel=PassedObjectOfMessageModel
xaml:
<TextBlock Name="lblFromUserName" Text="{Binding CurrentMessageModel.DisplaySender ,Mode=TwoWay}" Height="65" Style="{StaticResource PhoneTextNormalStyle}" FontSize="35"/>
<ItemsControl ItemsSource="{Binding Path=CurrentMessageModel.MessagesExchanged}">
//No need for data template as collection only contains string
<ItemsControl
<StackPanel Orientation="Horizontal" Grid.Row="1">
<TextBox Grid.Column="0" Name="txtNewMessage" Text="{Binding NewMessage,Mode=TwoWay}" Margin="0,0,0,0" Width="350"/>
<Button Grid.Column="1" Command="{Binding SendClickCommand,Mode=TwoWay}" Name="btnSend" Content="Send" Width="100" />
</StackPanel>
//C#
CurrentMessageModel.MessagesExchanged.Add(txtNewMessage.Text);
并且您不需要任何文本块来显示 ObservableCollection,因为您的集合仅包含字符串,因此只需将 ItemsSource 设置为集合,数据就会显示。
My model:
public class MyMessageModel
{
public string DisplaySender { get; set; }
//how does the below observable collection needs to be changed ,
//if I want to add another field to itemssource template.
//e.g. public DateTime Timestamp { get; set; }
public ObservableCollection<string> MessagesExchanged { get; set; }
public string NewMessage { get; set; }
}
Chat.xaml:
<TextBlock Name="lblFromUserName" Text="{Binding DisplaySender ,Mode=TwoWay}" Height="65" Style="{StaticResource PhoneTextNormalStyle}" FontSize="35"/>
<ItemsControl ItemsSource="{Binding Path=MessagesExchanged}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding ????,Mode=TwoWay}" />
<TextBlock Text="{Binding Path=Timestamp}" HorizontalAlignment="Right" VerticalAlignment="Bottom" Grid.Row="1"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl
<StackPanel Orientation="Horizontal" Grid.Row="1">
<TextBox Grid.Column="0" Name="txtNewMessage" Text="{Binding NewMessage,Mode=TwoWay}" Margin="0,0,0,0" Width="350"/>
<Button Grid.Column="1" Command="{Binding SendClickCommand,Mode=TwoWay}" Name="btnSend" Content="Send" Width="100" />
</StackPanel>
Chat.xaml.cs 如下所示:
public class Chat: PhoneApplicationPage
{
private MyMessageViewModel _MyMessageViewModel;
public Conversation()
{
InitializeComponent();
_MyMessageViewModel = new MyMessageViewModel();
this.DataContext = _MyMessageViewModel;
}
}
我的 ViewModel MyMessageViewModel 如下所示:
public System.Windows.Input.ICommand SendClickCommand
{
get
{
return new DelegateCommand((o) =>
{
Task.Factory.StartNew(() =>
{
//loop through the selected items and clear everything
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
try
{
//DO YOUR WORK HERE: TAKE THE NEW MESSAGE AND APPEND IT TO THE MESSAGES EXCHANGED
}
catch (Exception)
{
throw;
}
});
});
});
}
}
现在,当用户在上面的名为 Chat.xaml 的视图中(用户将从主页来到此页面)时,我想用顶部的 DisplaySender 值加载它,该值将在整个 conversation.Value 将此字段作为导航参数从主页传递。
并且每次用户单击“发送”按钮时,在 SendClickCommand 中仅通过从 txtNewMessage 字段添加新消息来更新 MessagesExchange 集合,然后清除该字段。
我这里有两个问题:
当用户第一次访问 Chat.xaml 时,我如何绑定三个字段的数据,例如DisplaySender(非空值将作为导航参数传递),MessagesExchange(最初在启动新对话时将为空,否则它将具有来自导航参数的非空值)和 NewMessage(最初这将始终为空)。
其次在 SendClickCommand 中通知 属性 我如何从 txtNewMessage 中获取文本并更新 ObservableCollection MessagesExchange 并在最后清除 txtNewMessage 的值。以及如何将 MessagesExchange 的值绑定到数据模板文本块字段?
我猜你试图在从主页导航到聊天页面时传递 Class MyMessageModel 的对象。
所以定义一个属性
private MyMessageModel currentMessageModel;
public MyMessageModel CurrentMessageModel
{
get { return currentMessageModel; }
set { currentMessageModel = value; }
}
并在 ChatPage 的 OnNavigatedTo 方法中设置
CurrentMessageModel=PassedObjectOfMessageModel
xaml:
<TextBlock Name="lblFromUserName" Text="{Binding CurrentMessageModel.DisplaySender ,Mode=TwoWay}" Height="65" Style="{StaticResource PhoneTextNormalStyle}" FontSize="35"/>
<ItemsControl ItemsSource="{Binding Path=CurrentMessageModel.MessagesExchanged}">
//No need for data template as collection only contains string
<ItemsControl
<StackPanel Orientation="Horizontal" Grid.Row="1">
<TextBox Grid.Column="0" Name="txtNewMessage" Text="{Binding NewMessage,Mode=TwoWay}" Margin="0,0,0,0" Width="350"/>
<Button Grid.Column="1" Command="{Binding SendClickCommand,Mode=TwoWay}" Name="btnSend" Content="Send" Width="100" />
</StackPanel>
//C#
CurrentMessageModel.MessagesExchanged.Add(txtNewMessage.Text);
并且您不需要任何文本块来显示 ObservableCollection,因为您的集合仅包含字符串,因此只需将 ItemsSource 设置为集合,数据就会显示。