1

我正在開發用於Windows Embedded項目的Silverlight。我定義了由TextBlock控件和圖像控件組成的自定義用戶控件。我想在自定義控件的不同實例中指定不同的文本。所以,我沒有在Expression Blend如下: 在混合代碼隱藏(C#)UserControl1.xaml.cs我定義和註冊一個DependencyProperty,並設置將TextBlock的DataContext:適用於Windows Embedded的Silverlight:如何將自定義屬性值從xaml傳播到VS項目嵌入式應用程序

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中我能看到T中的應用他更正了在自定義用戶控件的兩個實例上顯示的文本。

然後我將項目導入到Windows Embedded Application的Visual Studio Silverlight中。我閱讀了一些提及我必須重新註冊的帖子。所以,我做了以下內容:

在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中的文本。

我做錯了什麼或缺少什麼?我無法在因特網上找到關於此主題的許多信息。任何人都可以指出我正確的方向。謝謝!

回答

1

問題已解決。

我添加了一個回調XRDependencyPropertyMetaData.pfnPropertyChangeNotification:

dpm.pfnPropertyChangeNotification = NamePropertyChanged; 

根據MSDN,PFN_PROPERTY_CHANGE是XAML爲Windows嵌入式調用回調方法時的相關屬性的值更改。事實上,這個回調在初始化期間被調用。在回調,我設置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#代碼隱藏不同。

相關問題