1

ユーザーの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>

ボタンをクリックしてコマンドを実行すると、コードは実行されますが、画像のソースは更新されません。

ソースデータをバインドする方法に欠陥があると確信しています。

前もって感謝します。

4

1 に答える 1

2

DataContextページのを設定しているところはどこにもありません。適切に定義されDataContextていないと、使用しているバインディングをデータオブジェクトに解決できません。簡単な例は、OnNavigatedToメソッドまたはページのコンストラクターにあります。次のようなことを行う必要があります。

this.DataContext = new ViewModel()

または同様のもの。ビューモデルの作成や有効期間などをどのように処理するかによって、データコンテキストを接続する方法はさまざまですが、これで正しい方向に進むことができれば幸いです。page.DataContext = this;コードでは、コンストラクターを呼び出すこともできます。

更新:PropertyChangedイベントを発生させるときにもコードを修正する必要があります。あなたが持っている

if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("ImageUrl"));
            }

する必要があります

if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("AccountImageUrl"));
            }

プロパティ名に渡された文字列の変更に注意してください。存在しなかったプロパティに対して変更イベントを発生させていました。

更新#2:AccountImageUrlからあるタイプのユーザーを返しています。カスタムタイプではなく、実際のhttpurl文字列を返す必要があります。ユーザーがhttpUrlを指すプロパティを持っている場合は、XamlバインディングをAccountImageUrl.PropertyThatContainsRealUrlに更新できます。

アップデート#3:あなたのコードには

new User
                 {
                     AccountImageUrl = user.ProfileImageUrl
                 };

これは、新しいUserオブジェクトにAccountImageUrlを設定しています。ただし、ViewModelのAccountImageUrlにバインドしています。ある時点で、AccountImageURlを画像のURL文字列に設定し、AccountImageURLでPropertyChangedを発生させる必要があります。したがって、新しいユーザーを作成する前または後のいずれかでthis.AccountImageUrl = user.ProfileImageUrl. 、コードとコメントに加えた変更を正しくフォローしていることを願っています。

于 2012-09-08T23:52:14.220 に答える