0

我有很多 xaml 文件,需要使用一些我不想复制代码的函数:

主要的.xaml.cs

 void gotoUrL(..){}

 void goroUsers(..){}

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

我怎么能做到这一点?东西.xaml

<Image Tap="gotoUrl">

//gotoUrl 引用自 main.xaml.cs

4

2 回答 2

1

一种方法可能是您编写一个单独的辅助类:Helper.cs. (您可以将其设为静态)。中Helper.cs,您可以放置​​所有不想复制的功能。然后调用那个特定的辅助类函数:Helper.gotoUrl()从and的gotoUrl()方法。您的代码不再重复。main.xaml.cssomething.xaml.cs

于 2012-06-02T19:33:19.207 回答
0

你有几个选项UserControls.. 如果你只需要一个特定的图像,它会保持不变,你可以简单地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>

我不在一台可以为您提供完整工作代码的计算机上,但是此链接应填写缺失的部分。

如何实现依赖属性

于 2012-06-02T20:09:36.053 回答