适用于Windows Embedded的Silverlight:如何将自定义属性值从xaml传播到C ++自定义控件 [英] Silverlight for Windows Embedded: How to propagate custom property value from xaml to C++ custom control

查看:48
本文介绍了适用于Windows Embedded的Silverlight:如何将自定义属性值从xaml传播到C ++自定义控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发Silverlight for Windows Embedded项目。我定义了一个自定义用户控件,它由一个TextBlock控件和一个图像控件组成。我想在自定义控件的不同实例中指定
不同的文本。所以我在Expression Blend中执行了以下操作:在Blend代码隐藏(C#)UserControl1.xaml.cs中我定义并注册了DependencyProperty并设置了TextBlock的DataContext:

namespace WindowsEmbeddedSilverlightApplication1
{
    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
            ItemText.DataContext = this;
        }

        public String MyText
        {
            get { return (String)GetValue(MyTextProperty); }
            set { SetValue(MyTextProperty, value); }
        }

        public static readonly DependencyProperty MyTextProperty =
            DependencyProperty.Register("MyText", typeof(String), typeof(UserControl1), null);
    }
}

在UserControl1.xaml中:

<UserControl
    ......
    x:Class="WindowsEmbeddedSilverlightApplication1.UserControl1"
    d:DesignWidth="196" d:DesignHeight="85">

    <Grid x:Name="LayoutRoot">
        <Image x:Name="ItemImage" Margin="0,0,90,0"/>
        <TextBlock x:Name="ItemText" HorizontalAlignment="Right" Width="68" Text="{Binding MyText}" TextWrapping="Wrap" Height="23" VerticalAlignment="Bottom"/>
    </Grid>
</UserControl>

在MainPage.xaml中使用自定义用户控件:

<UserControl
    ......
    xmlns:local="clr-namespace:WindowsEmbeddedSilverlightApplication1"
    x:Class="WindowsEmbeddedSilverlightApplication1.MainPage"
    Width="640" Height="480">

    <Grid x:Name="LayoutRoot" Background="White">
        <local:UserControl1 HorizontalAlignment="Left" Margin="94,117,0,0" Width="196" Height="85" VerticalAlignment="Top" MyText="Text1"/>
        <local:UserControl1 HorizontalAlignment="Left" Margin="94,217,0,0" Width="196" Height="85" VerticalAlignment="Top" MyText="Text2"/>
    </Grid>
</UserControl>





因此,当我在Expression Blend中运行应用程序时,我能够看到正确的文本显示在自定义用户控件的两个实例上。


So with these when I run the application in Expression Blend I am able to see the correct text displayed on the two instances of the custom user control.


然后我将项目导入Visual Studio Silverlight for Windows Embedded Application。我读了一些帖子,提到我必须重做注册。所以我做了以下几点:

Then I import the project into Visual Studio Silverlight for Windows Embedded Application. I read some posts mentioning that I have to redo the registeration. So I did the following:


在UserControl1.h中:

In UserControl1.h:

    static HRESULT Register()
    {
        HRESULT hr = E_FAIL;
        hr = XRCustomUserControlImpl<UserControl1>::Register (__uuidof(UserControl1), L"UserControl1", L"clr-namespace:WindowsEmbeddedSilverlightApplication1");
        hr = RegisterDependencyProperties();
        return hr;
    }

    static HRESULT RegisterDependencyProperties();

    HRESULT SetMyText(WCHAR* pText);
    HRESULT GetMyText(BSTR* pbstrText);

public:
    static DEPENDENCY_PROPERTY_ID m_dpMyTextID;





在UserControl1.cpp中:

HRESULT UserControl1::RegisterDependencyProperties()
{
    HRESULT hr = E_FAIL;
    IXRApplication* pApplication = NULL;

    XRDependencyPropertyMetaData dpm = XRDependencyPropertyMetaData();
    dpm.Size = sizeof(XRDependencyPropertyMetaData);
    dpm.pfnPropertyChangeNotification = NULL;
    dpm.pfnTypeConverter = NULL;
    dpm.pfnEnumerableCreation = NULL;

    XRValue defValue;
    defValue.vType = VTYPE_READONLY_STRING;
    defValue.pReadOnlyStringVal = L"Default";
    dpm.DefaultValue = defValue;

    hr = GetXRApplicationInstance(&pApplication);
    hr = pApplication->RegisterDependencyProperty(L"MyText", VTYPE_BSTR, ControlID(), &dpm, &m_dpMyTextID);

    pApplication->Release();

    return hr;
}

HRESULT UserControl1::SetMyText( WCHAR* pText )
{
    HRESULT hr = E_FAIL;

    XRValue xrValue;
    xrValue.vType = VTYPE_READONLY_STRING;
    xrValue.pReadOnlyStringVal = pText;

    hr = SetPropertyValue(m_dpMyTextID, &xrValue);

    return hr;
}

HRESULT UserControl1::GetMyText( BSTR* pbstrText )
{
    HRESULT hr = E_FAIL;

    XRValue xrValue;

    hr = GetPropertyValue(m_dpMyTextID, &xrValue);

    if (SUCCEEDED(hr))
    {
        *pbstrText = xrValue.bstrStringVal;
    }

    return hr;
}





我没有更改生成的MainPage.h和MainPage.cpp代码中的任何内容。


I didn't change anything in the generated MainPage.h and MainPage.cpp code.


编译成功,执行也正常。显示自定义控件,但不会显示我放在xaml中的文本。

Compilation is successful, and execution is also ok. The custom controls are displayed, but the texts that I put in the xaml are not displayed.


我做错了什么或丢失了什么?我在互联网上找不到关于这个主题的很多信息。任何人都可以指出我正确的方向。谢谢!

Am I doing something wrong or missing something? I couldn't find much information on this topic on the Internet. Could anyone point me in the right direction. Thanks!


推荐答案

问题解决了。



我向XRDependencyPropertyMetaData.pfnPropertyChangeNotification添加了一个回调:

Problem solved.

I added a callback to XRDependencyPropertyMetaData.pfnPropertyChangeNotification:
    dpm.pfnPropertyChangeNotification = NamePropertyChanged;


根据MSDN,PFN_PROPERTY_CHANGE是一个回调方法,当关联属性的值发生更改时,Windows Embedded的XAML会调用该方法。实际上,在初始化期间调用此回调。在回调中,我将TextBlock文本设置为新的
值:

According to MSDN, PFN_PROPERTY_CHANGE is a callback method that XAML for Windows Embedded invokes when the value of the associated property changes. Indeed this callback is called during initialization. In the callback, I set the TextBlock text to the new value:

void UserControl1::NamePropertyChanged(__in IXRDependencyObject* pControl, __in XRValue* pOldValue, __in XRValue* pNewValue)
{
	HRESULT hr;
	if (pControl && pOldValue && pNewValue)
	{
		UserControl1 *tempObj;
		pControl->QueryInterface(__uuidof(UserControl1), (void**)&tempObj);
		hr = tempObj->m_pItemText->SetText(pNewValue->pReadOnlyStringVal);
	}
}


因此,在C ++代码隐藏中将DependencyProperty值绑定到控件的方式与C#代码隐藏不同。

So the way of binding DependencyProperty value to a control in C++ code-behind is different from C# code-behind.


这篇关于适用于Windows Embedded的Silverlight:如何将自定义属性值从xaml传播到C ++自定义控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆