我正在尝试获取用户 profileImageURl 并将其绑定到图像。我有以下 C# 代码:
namespace IIVVYTwitter
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
DataContext = new ViewModel(this);
}
}
class ViewModel : INotifyPropertyChanged
{
public User AccountImageUrl { get; set; }
readonly Page page;
public ViewModel(Page page)
{
this.page = page;
LoadAccount = new TwitterCommand<object>(LoadAccountHolder);
}
public TwitterCommand<object> LoadAccount { get; set; }
void LoadAccountHolder(object obj)
{
PinAuthorizer auth =
new PinAuthorizer
{
Credentials = new LocalDataCredentials()
};
if (auth == null || !auth.IsAuthorized)
{
page.Frame.Navigate(typeof(oAuth));
return;
}
var twitterCtx = new TwitterContext(auth);
var accounts =
from acct in twitterCtx.Account
where acct.Type == AccountType.VerifyCredentials
select acct;
Account account = accounts.SingleOrDefault();
LinqToTwitter.User user = account.User;
new User
{
AccountImageUrl = user.ProfileImageUrl
};
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("AccountImageUrl"));
}
}
}
}
与此 XAML 链接:
<Page
x:Class="IIVVYTwitter.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:IIVVYTwitter"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid>
<Button Command="{Binding LoadAccount}">
<Border>
<Image x:Name="accountPicture" Source="{Binding AccountImageUrl}" />
</Border>
</Button>
</Grid>
</Page>
当我通过单击按钮运行命令时,代码运行但图像的源没有更新。
我很确定我绑定源数据的方式是有缺陷的。
提前致谢。