我為用C /WinRT撰寫的自定義XAML控制元件撰寫的IDL中必須有哪些屬性/函式/任何東西?
舉例說明
。當我在Visual Studio中創建一個自定義的C /WinRT XAML控制元件時,它創建了4個檔案:
當我在Visual Studio中創建一個自定義的C /WinRT XAML時,它創建了4個檔案
- MyControl.xamL
- MyControl.xaml
- MyControl.idl
- MyControl.h
- MyControl.cpp
- MyControl.cpp 。
IDL檔案是基本的,包括一個簡單的屬性:
namespace MyNamespace
{
[default_interface]/span>
runtimeclass MyControl : Windows.UI.Xaml.Controls.UserControl
{
MyControl()。
Int32 MyProperty。
}
有趣的是,.h檔案除了這個屬性外,還包括另一個函式:
#pragma once
#include "winrt/Windows.UI.Xaml.h"
#include "winrt/Windows.UI.Xaml.Markup.h"
#include "winrt/Windows.UI.Xaml.Interop.h"
#include "winrt/Windows.UI.Xaml.Controls.Primitives.h"/span>
#include "MyControl.g.h"
namespace winrt::MyNamespace::implementation
{
struct MyControl : MyControlT< MyControl>
{
MyControl()。
int32_t MyProperty()。
void MyProperty(int32_t value)。
void ClickHandler(Windows::Foundation:: IInspectable const& sender, Windows::UI::Xaml::RoutedEventArgs const& args>。
};
}
namespace winrt::MyNamespace::factory_implementation
{
struct MyControl : MyControlT<MyControl, implementation::MyControl>
{
};
其中的.xaml與之系結沒有問題:
<!-- ... -->
<Button x: Name="Button" Click="ClickHandler"> 點擊我</按鈕>。
<!-- ... -->
問題
我不清楚什么時候需要在我的IDL上引入一個函式或屬性,以便在XAML中使用它。是否所有屬性都需要被添加到 IDL 中?僅僅是依賴性屬性嗎?函式是否必須出現在 IDL 中?
為什么MyProperty會出現在IDL中,而不是ClickHandler?
謝謝!
uj5u.com熱心網友回復:
簡短的回答:
| 需要在IDL中實作嗎? | 需要用C 實作嗎? | ||
|---|---|---|---|
| 屬性 |
x:Bindx:Bind系結ICustomPropertyProvider和ICustomProperty。參見使用C /WinRT的{Binding}標記擴展。x:Bindx:Bindx:Bind?是的,該元素必須在IDL中暴露出來。
或者另一種說法:
任何使用
x:Bind的東西都需要在IDL中出現。- 這包括使用
x:Bind的一切--屬性和函式是明顯的,但也包括元素(見元素到元素系結)。因此,如果您想將x:Bind一個TextBlock的Content系結到一個TextBox的Text,您將需要在IDL中暴露TextBox。
- 這包括使用
其他所有的東西都不需要在IDL中出現。
要使用老式的(而且效率較低&更脆)
。系結,你的控制元件必須實作ICustomPropertyProvider和ICustomProperty。參見使用C /WinRT的{Binding}標記擴展。如果你將一個函式作為一個委托來參考(例如,
PointerMoved="MyTextBlock_PointerMovedHandler"),該函式只需成為你的控制元件類中的一個公共方法。
MSDN的XAML控制元件;系結到C /WinRT屬性提供了完整的答案:
通過使用 XAML {x:Bind}標記擴展所消耗的所有物體必須在 IDL 中公開暴露。此外,如果XAML標記包含對另一個也在標記中的元素的參考,那么該標記的getter必須在IDL中出現。
<Page x:Name="MyPage"> <StackPanel>/span> <CheckBox x: Name="UseCustomColorCheckBox" Content="使用自定義顏色" Click="UseCustomColorCheckBox_Click"/span> /> <Button x: Name="ChangeColorButton" Content="Change color" Click="{x:Bind ChangeColorButton_OnClick}" IsEnabled="{x:Bind UseCustomColorCheckBox.IsChecked.Value, Mode=OneWay}"/span>/> </StackPanel>/span> </Page>/span>ChangeColorButton元素通過系結參考到UseCustomColorCheckBox元素。所以這個頁面的IDL必須宣告一個名為UseCustomColorCheckBox的只讀屬性,以使它能夠被系結訪問。
UseCustomColorCheckBox的點擊事件處理委托使用了經典的XAML委托語法,因此不需要在IDL中加入條目;它只需要在你的實作類中公開。另一方面,ChangeColorButton也有一個{x:Bind}點擊事件處理程式,它也必須進入IDL中。
runtimeclass MyPage : Windows.UI.Xaml.Controls.Page { MyPage()。 //這些成員被系結消耗。 void ChangeColorButton_OnClick()。 Windows.UI.Xaml.Controls.CheckBox UseCustomColorCheckBox{ get; };你不需要為UseCustomColorCheckBox屬性提供一個實作。XAML代碼生成器將為您做到這一點。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/312690.html
標籤:
