簡體   English   中英

來自另一個XAML的動作

[英]Action from another xaml

我有許多xaml文件,需要使用一些我不想重復代碼的功能:

main.xaml.cs

 void gotoUrL(..){}

 void goroUsers(..){}

main.xaml
 <Image Tap="gotoUrl">..

我該怎么做? 東西

<Image Tap="gotoUrl">

//從main.xaml.cs引用的gotoUrl

一種方法是編寫一個單獨的幫助程序類: Helper.cs (您可以將其設為靜態)。 Helper.cs ,您可以放置​​所有不想重復的功能。 然后調用該特定輔助類功能: Helper.gotoUrl()gotoUrl()的方法main.xaml.cssomething.xaml.cs 您的代碼不再重復。

您可以使用UserControls選擇。.如果只需要一個將保持不變的特定Image,則可以簡單地在其中包含Image的情況下制作一個新的UserControl

如果功能可能有所不同,則可以使UserControl像這樣(我們稱之為GoToUrlControl):

//all the xaml at the top of the file

x:Name="Control">

<Grid Tap="OnTapped">
   <ContentControl Content="{Binding Path=Body, ElementName=Control}"/>
</Grid>

然后,在后面的代碼中,創建一個DependencyProperty (鍵入propdp並點擊兩次tab ),將其命名為Body

最后,要在任何xaml頁面上使用它,請添加xaml參考,以及類似的內容

<myControls:GoToUrlControl>
   <GoToUrlControl.Body>
      <//put any content here here />
   </GoToUrlControl.Body>
</myControls:GoToUrlControl>

我不在能為您提供完整工作代碼的計算機上,但是此鏈接應該可以填寫缺失的部分。

如何實現依賴項屬性

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM