2012-11-16 34 views
0

我想創建我的C++/CX的WinRT項目的附加屬性。當我這樣做時,XAML編譯器會抱怨我正在使用未知屬性。如何創建和C++/CX和XAML使用AttachedProperty?

我聲明瞭一個CPP文件我的附加屬性爲這樣:

/*-------------------------------------------------------------------- 
    Forward Declarations 
--------------------------------------------------------------------*/ 
namespace 
{ 
    void 
    StateChanged(
     DependencyObject^ target, 
     DependencyPropertyChangedEventArgs^ args); 
} 

/*-------------------------------------------------------------------- 
    DependancyProperty setup for the view model 
--------------------------------------------------------------------*/ 
namespace 
{ 
DependencyProperty^ _stateProperty = 
    DependencyProperty::RegisterAttached(
     "State", 
     TypeName(Platform::String::typeid), 
     TypeName(VisualStateManagerBindingHelper::typeid), 
     ref new PropertyMetadata(nullptr, ref new PropertyChangedCallback(&StateChanged))); 
} 

namespace 
{ 
    void 
    StateChanged(
     DependencyObject^ target, 
     DependencyPropertyChangedEventArgs^ args) 
    { 
     if (args->NewValue != nullptr) 
     { 
      VisualStateManager::GoToState(safe_cast<Control^>(target), safe_cast<String^>(args->NewValue), true); 
     } 
    } 
} 

在我的XAML文件,我嘗試使用附加屬性爲這樣:

<UserControl 
    xmlns:local="using:MyCustomNamespace" 
    ... 

    <Grid Grid.Column="1" local:VisualStateManagerBindingHelper.State="{Binding Path=SomeViewModelProperty, Mode=TwoWay}"> 
     ... 

當我編譯,我遇到了這個錯誤:

XamlCompiler錯誤WMC0010:元素'網格'上的未知可附加成員'VisualStateManagerBindingHelper.State'

在我看來,好像XAML編譯器應該在某處提示該附加屬性是允許的,但我不知道如何給它提示。關於同一主題的This article表明DependancyProperty應該公開。我不完全確定如何在C++中執行此操作。

我的最終目標是將視覺狀態組的狀態結合到一些視圖模型屬性。在this question的接受答案中概述了執行此操作的方法。不幸的是,在那裏提供的代碼是C#,我似乎無法正確地將它轉換爲C++。

最後,我注意到,隨Visual Studio的新項目模板定義了一個附加屬性幾乎完全一樣我做SuspensionManager,但他們不顯示在使用它的任何實例。這讓我覺得自己在走上正軌,但錯過了一小謎團。

回答

0

我發現,回答我的問題一個網站!正如我懷疑的那樣,您需要以非常特定的方式定義您的類,以便XAML預處理器瞭解您的附屬屬性。所有要求和代碼示例均可用here