使用MVVM模式中,應用程序的UI和底層表示和業務邏輯.XAML和代碼: 的TextBlocks,圖像這樣定義:無法綁定到mvvm中的文本塊?
<Page.DataContext>
<ViewModel:TweetVM/>
</Page.DataContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="100"/>
<RowDefinition/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Orientation="Horizontal" >
<Image x:Name="Thumbnail" Source="{Binding GetAccount.ProfileImgUrl}" Width="100" Margin="8"/>
<StackPanel Margin="8">
<TextBlock x:Name="UserName" Text="{Binding GetAccount.Name}" Foreground="#262626" VerticalAlignment="Top"/>
<TextBlock x:Name="AccountName" Text="{Binding GetAccount.ScreenName}" Foreground="#666666"/>
</StackPanel>
</StackPanel>
CODE: 這裏是全碼:
public class TweetVM : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private Accounts getAccount;
public Accounts GetAccount {
get { return getAccount; }
set
{
getAccount = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("GetAccount"));
}
}
public TweetVM()
{
GetProfile();
}
/// <summary>
///
/// </summary>
public async void GetProfile()
{
try
{
GetAccount = new Accounts();
var profile = await(from index in twtContext.Account
where index.Type == AccountType.VerifyCredentials
select index).SingleOrDefaultAsync();
GetAccount.Name = profile.User.Name;
GetAccount.ProfileImgUrl = profile.User.ProfileImageUrl;
GetAccount.ScreenName = profile.User.ScreenNameResponse;
}
catch (Exception ex)
{
errorDialog = new ErrorDialog(ex.Message, "Error : ");
}
}
GetAccount獲取了數據,但沒有看到綁定到textblock,...有什麼問題? 我不明白問題出在哪裏?
檢查'BindingMode' – AnjumSKhan
你的問題是'異步void'方法。因爲方法沒有返回'Task',所以你不能觀察結果。如果'GetProfile'方法會拋出異常 - 什麼都不會發生 - 什麼顯然在你的情況下被削弱 – Fabio
沒有errors.var profile = await(來自twtContext.Account中的索引... –