Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何创建动态图像控制metro应用程序_C#_Windows 8_Microsoft Metro_Dynamic Controls - Fatal编程技术网

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

C# 如何创建动态图像控制metro应用程序,c#,windows-8,microsoft-metro,dynamic-controls,C#,Windows 8,Microsoft Metro,Dynamic Controls,我正试图从显示我所有xbox live好友信息的XML文档中获取信息。我现在想做的是在动态创建的图像控件中显示阿凡达,但我不确定如何在我的应用程序网格中实际显示该图像 到目前为止,我已经尝试使用gamertag创建一个动态控件,并向其中添加我的自定义文本。这是迄今为止的代码: string gamertag, avatarURL; foreach (XElement elm in doc.Descendants().Elements("Friends"))

我正试图从显示我所有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>

在xaml中这样做会更容易:

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

}
首先,将页面的
DataContext
设置为viewmodel。然后创建一个属性,将您的好友集合公开为一组对象,这些对象公开了
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));
        }
    }

}
public类yourCodeThatGetsYourFriends:INotifyPropertyChanged
{
公共事件属性更改事件处理程序属性更改;
公开作废GetFriends()
{
//获取好友并放入下面定义的好友对象中
//创建它们的ObservableCollection并将其分配给Friends(确保其“Friends”而不是“Friends”)属性
}
公众观察收集朋友;
公众观察收集朋友;
{
得到
{
回报朋友;
}
设置
{
朋友=价值;
通知财产变更(“朋友”);
}
}        
私有void NotifyPropertyChanged([CallerMemberName]字符串propertyName=”“)
{
if(PropertyChanged!=null)
{
PropertyChanged(这是新的PropertyChangedEventArgs(propertyName));
}
}
}
公共类好友:INotifyPropertyChanged
{        
公共事件属性更改事件处理程序属性更改;
字符串url;
公共字符串
{
得到
{
返回url;
}
设置
{
url=值;
NotifyPropertyChanaged(“化身”);
}
}
字符串标签;
公共字符串玩家代号
{
得到
{
返回标签;
}
设置
{
标签=值;
NotifyPropertyChanaged(“玩家代号”);
}
}
私有void NotifyPropertyChanged([CallerMemberName]字符串propertyName=”“)
{
if(PropertyChanged!=null)
{
PropertyChanged(这是新的PropertyChangedEventArgs(propertyName));
}
}
}

为什么不在xaml中执行此操作?在xaml中执行类似操作要容易得多。我有一个xaml设计,但这是背后的代码,我如何使用xaml执行此操作?请查看原始问题,并添加代码。我找不到与您提到的相同的控件,因此我使用了我认为最好的控件ItemsControl I想想mydogisbox试图解释的是:1)需要创建一个具有某些属性的类,如GamerTag、avatarURL等。2)解析XML并用解析的数据填充类。3)在代码中,使用刚刚创建的类的实例设置DataContext。你是ASP.NET开发人员吗?普通的ASP.NET开发人员是否尝试查找控件当他们启动wpf学习时,在代码隐藏中。在wpf中,规则是:让xaml完成设计任务,在代码隐藏中,您必须(优先)执行业务代码。有更好的方法可以做到这一点,但我只是给出了一个基本的viewmodel示例。