我正在研究一個「現代UI」應用程序,所以語法對我來說有點新,我似乎無法讓我的綁定正常工作。MVVM的DataTemplate:它到哪裏去了?
我的願望是有一個ViewModel第一設計讓我的應用程序的網頁我可以做這樣的事情有ListView
並添加UserViewModel
作爲一個孩子,並有DataTemplate中自動發現來創建用戶視圖,並綁定到提供的UserViewModel。
我針對爲Win 7桌面編寫的另一個應用程序做了類似的工作,它只是起作用,但對於我的生活我無法弄清楚爲什麼它不起作用。我只是在我的ListView「UserViewModel」作爲文本(沒有UserControl創建)。
這裏唯一的區別是它是我第一次使用異步函數,因爲它在Win 8開發中非常強迫你,而且這是我從WCF服務中獲得的方法數據來自。
這裏是我的視圖模型的例子:
public class UserViewModel
{
private UserDTO _user { get; set; }
public UserViewModel(UserDTO user)
{
_user = user;
}
public UserViewModel(int userId)
{
SetUser(userId);
}
private async void SetUser(int userId)
{
ServiceClient proxy = new ServiceClient();
UserDTO referencedUser = await proxy.GetUserAsync(userId);
}
public string FirstName
{
get
{
return _user.FirstName;
}
}
public string LastName
{
get
{
return _user.LastName;
}
}
public string Email
{
get
{
return _user.email;
}
}
}
的觀點應該是所有XAML和應用程序資源粘在一起如下:
<UserControl x:Class="TaskClient.Views.UserView" ...
xmlns:root="using:TaskClient"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="30"
d:DesignWidth="200">
<StackPanel Orientation="Horizontal" Margin="5, 0, 0 ,0" DataContext="{Binding}">
<TextBlock x:Name="FirstNameLabel" Text="{Binding FirstName}"/>
<TextBlock x:Name="LastNameLabel" Text="{Binding LastName}"/>
<TextBlock x:Name="EmailLabel" Text="{Binding Email}"/>
</StackPanel>
</UserControl>
和:
<Application x:Class="TaskClient.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:TaskClient"
xmlns:localData="using:TaskClient.Data"
xmlns:vm="using:ViewModels"
xmlns:vw="using:TaskClient.Views">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!--
Styles that define common aspects of the platform look and feel
Required by Visual Studio project and item templates
-->
<ResourceDictionary Source="Common/StandardStyles.xaml"/>
</ResourceDictionary.MergedDictionaries>
<!-- Application-specific resources -->
<x:String x:Key="AppName">TaskClient</x:String>
<DataTemplate x:Key="vm:UserViewModel">
<vw:UserView />
</DataTemplate>
</ResourceDictionary>
</Application.Resources>
我試過通過各種例子搜索一個小時左右(例如。 http://joshsmithonwpf.wordpress.com/a-guided-tour-of-wpf/),並沒有找到一個適用於我的案例。
任何想法我做錯了什麼?
Modern UI ap沒有x:Type屬性這是我的問題。不知道它應該是什麼方式。 – Mike
@Mike我沒有意識到這一點。你可以使用DataTemplateSelectors嗎?這是我在Silverlight中添加對隱式數據模板的支持之前使用的。也許像[this](http://www.wiredprairie.us/blog/index.php/archives/1705)會起作用? – Rachel
嗨,是的,這可能會工作。無論如何,我必須在數據綁定之前解決一些異步加載問題。請參閱http://stackoverflow.com/a/14189904/1462330上的相關問題的答案。基本上,我現在在我的viewmodels上有一個「初始化」任務屬性。當您創建其中的一個時,您將等待初始化的任務,以便所有WCF資料都被加載並分配屬性。我在視圖的初始化任務中做了類似的事情,除了在他們結束時,我將它們綁定到他們剛剛初始化的東西似乎運作良好。 – Mike