0

すべての xbox ライブ フレンド情報を示す XML ドキュメントから情報を取得しようとしています。私が今やりたいことは、動的に作成された画像コントロールにアバターを表示することですが、実際にその画像をアプリグリッドに表示する方法がわかりません。

これまでのところ、ゲーマータグを使用して動的コントロールを作成し、それにカスタム テキストを追加しようとしました。これまでのコードは次のとおりです。

        string gamertag, avatarURL;
        foreach (XElement elm in doc.Descendants().Elements("Friends"))
        {

            gamertag = elm.Element("Gamertag").Value;
            avatarURL = elm.Element("AvatarLarge").Value;

            Image friendimage = new Image();
            friendimage.Name = gamertag.ToString() + "ImageControl";

            BitmapImage AccountPicbitmap = new BitmapImage();
            AccountPicbitmap.UriSource = new Uri(avatarURL);

            friendimage.Source = AccountPicbitmap;
            //Some code to display this control with the avatar image using the URL retrieved, I want to play these tiles side by side

        }

これを行う方法について何か提案はありますか? 前もって感謝します!

更新: このコントロールを XAML に追加しましたが、奇妙な例外がいくつか発生しています: System.Runtime.Remoting.RemotingException [7756] Designer プロセスが予期せず終了しました!

<ItemsControl HorizontalAlignment="Left" Height="100" VerticalAlignment="Top" Width="1249" Margin="55,484,0,0" ItemsSource="{Binding}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Image Source="{Binding avatarURL}" Name="{Binding GamerTag}"/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>

</ItemsControl>

アプリをデバッグすると、無限ループに入り、初期化でも例外がスローされます

public MainPage()
    {
        this.InitializeComponent();
    }
4

1 に答える 1

0

これは、次のような xaml で行う方が簡単です。

まず、DataContextページの をビューモデルに設定します。次に、フレンドのコレクションを、GamerTagおよびを公開するオブジェクトのコレクションとして公開するプロパティを作成しますavatarURL。このコレクションを表示するには、次の xaml を使用します。

<ItemsControl ItemsSource="{Binding}">
    <ItemsControl.DataTemplate>
        <DataTemplate>
            <Image Source="{Binding avatarURL}" Name="{Binding GamerTag}"/>
        </DataTempate>
    </ItemsControlDataTempalte>
</ItemsControl>

コードビハインドは次のようになります。

public class yourCodeThatGetsYourFriends : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public void GetFriends()
    {
        //get friends and put into friend objects defined below
        //create an ObservableCollection of them and assign it to the Friends (make sure its 'Friends' not 'friends") property
    }
    public ObservableCollection<friend> friends;
    public ObservableCollection<friend> Friends;
    {
        get
        {
            return friends;
        }
        set
        {
            friends = value;
            NotifyPropertyChanged("Friends");
        }
    }        

    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

}

public class friend : INotifyPropertyChanged
{        
    public event PropertyChangedEventHandler PropertyChanged;

    string url;
    public string avatarURL
    {
        get
        {
            return url;
        }
        set
        {
            url = value;
            NotifyPropertyChanaged("avatarURL");
        }
    }
    string tag;
    public string GamerTag
    {
        get
        {
            return tag;
        }
        set
        {
            tag= value;
            NotifyPropertyChanaged("GamerTag");
        }
    }

    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

}
于 2012-10-07T20:22:33.733 に答える