我正在开发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中运行应用程序时,我能够看到自定义用户控件的两个实例上显示的正确文本。
然后我将项目导入Visual Studio Silverlight for Windows Embedded Application。我读了一些帖子,提到我必须重做注册。所以我做了以下事情:
在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代码中更改任何内容。
编译成功,执行也正常。显示自定义控件,但不显示我放入xaml的文本。
我做错了什么或遗失了什么?我在互联网上找不到关于这个主题的很多信息。任何人都可以指出我正确的方向。谢谢!
答案 0 :(得分:1)
问题解决了。
我添加了对XRDependencyPropertyMetaData.pfnPropertyChangeNotification的回调:
dpm.pfnPropertyChangeNotification = NamePropertyChanged;
根据MSDN,PFN_PROPERTY_CHANGE是一种回调方法,当关联属性的值发生更改时,Windows Embedded的XAML会调用该方法。实际上,在初始化期间调用此回调。在回调中,我将TextBlock文本设置为新值:
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#代码隐藏不同。