如何创建动态图像控制metro应用程序

时间:2012-10-07 05:27:24

标签: c# windows-8 microsoft-metro dynamic-controls

我正在尝试从显示所有xbox live朋友信息的XML文档中获取信息。我现在要做的是在动态创建的图像控件中显示头像,但我不确定如何在我的应用网格上显示该图像。

到目前为止,我已尝试使用gamertag创建动态控件并将自定义文本添加到其中。这是到目前为止的代码:

        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();
    }

1 个答案:

答案 0 :(得分:0)

这在xaml中更容易做到这样:

首先,将您网页的DataContext设置为您的viewmodel。然后创建一个属性,将您的朋友集合公开为公开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));
        }
    }

}