WP8.1 -用户控制文本框+图像,不显示数据

本文关键字:图像 显示 数据 用户 控制 文本 WP8 | 更新日期: 2023-09-27 17:49:39

我很可能错过了一些明显的东西,因为我是WPF开发的新手。我试图创建一个Windows Phone 8.1应用程序,为我的使用,我想创建一个包含英雄信息和英雄图标的自定义用户控件(简单的游戏相关信息查找应用程序)。

我创建了一个名为HeroInformationControl的用户控件,然后在XAML中定义了一个图像和文本块。通过各种在线资源查找,我创建了它如下:

<UserControl Name="HeroInformationControl"
    x:Class="DotaHelper.HeroInformation"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:DotaHelper"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="50"
    d:DesignWidth="400">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="1*"></ColumnDefinition>
        <ColumnDefinition Width="3*"></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <Image           
        HorizontalAlignment="Left" 
        Height="Auto"
        Stretch="Fill"
        VerticalAlignment="Top"
        Width="Auto" Source="{Binding ElementName=HeroInformationControl, Path=HeroImage}"
        />
    <TextBlock             
        Grid.ColumnSpan="2" 
        HorizontalAlignment="Center"
        Height="50"
        Grid.Column="1"
        TextWrapping="Wrap"
        Text="{Binding ElementName=HeroInformationControl, Path=HeroName}"
        VerticalAlignment="Center"
        Width="300"/>

然后,HeroInformation.xaml.cs:

public partial class HeroInformation
{
    public HeroInformation()
    {
        this.InitializeComponent();
        this.DataContext = this;
    }
    public static readonly DependencyProperty HeroNameProperty =
          DependencyProperty.Register("HeroName", typeof(string), typeof(string), new PropertyMetadata(""));
    public string HeroName
    {
        get { return (string)GetValue(HeroNameProperty); }
        set { SetValue(HeroNameProperty, value); }
    }
    public static readonly DependencyProperty HeroImageProperty =
         DependencyProperty.Register("HeroImage", typeof(string), typeof(string), new PropertyMetadata(""));
    public string HeroImage
    {
        get { return (string)GetValue(HeroImageProperty); }
        set { SetValue(HeroImageProperty, value); }
    }
}

主页。xaml, HeroInformation对象:

            <local:HeroInformation
                x:Name="HeroInformation1"
                HorizontalAlignment="Left"
                Height="Auto"
                VerticalAlignment="Top" 
                Width="200"
                />

MainPage.xaml.cs的UI线程:

            HeroInformation1.HeroImage = hero.IMGurl;
            HeroInformation1.HeroName = hero.Heroname;

很抱歉长代码,但我几乎不知道问题在哪里。注意:hero.IMGUrlhero.Heroname性质都是字符串。另外,如果我手动添加Mainpage.xaml属性(HeroImageHeroName),它会加载。

任何帮助理解什么是错误的将是感激的-同样,如果你发现一些离最佳编程实践很远的东西,我将感激提示。

WP8.1 -用户控制文本框+图像,不显示数据

从不。永远。永远。这样做:

this.DataContext = this;
相反,在XAML文件中给UserControl一个x:Name。这样的:
<UserControl x:Name="usr" ... >

这将允许你使用以下绑定绑定到你的依赖属性:

Text="{Binding DataContext.HeroName, ElementName=usr}"

或者,您可以使用以下命令将UserControl绑定到自身:

DataContext="{Binding RelativeSource={RelativeSource Self}}"

你的绑定看起来像这样:

Text="{Binding HeroName}"

编辑:同样,正如Juan注意到的,你的依赖属性声明是不正确的:

public string HeroName
    {
        get { return (string)GetValue(HeroNameProperty); }
        set { SetValue(HeroNameProperty, value); }
    }
    // Using a DependencyProperty as the backing store for HeroName.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty HeroNameProperty =
        DependencyProperty.Register("HeroName", typeof(string), typeof(HeroInformation), new PropertyMetadata(null));

专业提示:使用propdp -> Tab -> Tab来声明依赖属性

你唯一错过的是:

public static readonly DependencyProperty HeroNameProperty =
      DependencyProperty.Register("HeroName", typeof(string), typeof(HeroInformation), new PropertyMetadata(""));
typeof(HeroInformation)

剩下的是完美的和做它的方式。可能是图像给出了一些问题(我不记得在所有平台(WPF,WP, UWP),如果它是正确的使用字符串的URI。