0

我正在尝试从显示我所有的 Xbox Live 好友信息的 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] 设计器进程意外终止!

<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您的页面设置为您的视图模型。然后创建一个属性,将您的朋友集合公开为对象集合,这些对象集合GamerTagavatarURL. 使用以下 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 回答