我正在使用通用应用程序(Windows 8.1)。
我想通过点击UserControl中的按钮来增加主页中的“Number”属性。
(上面的代码就是一个例子)
Mainpage.xaml:
<Page
x:Class="App3.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App3"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:uc="using:App3"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid>
<uc:MyUserControl1></uc:MyUserControl1>
</Grid>
</Page>
Mainpage.xaml.cs:
namespace App3
{
public sealed partial class MainPage : Page
{
public int number { get; set; }
public MainPage()
{
this.InitializeComponent();
this.NavigationCacheMode = NavigationCacheMode.Required;
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
}
}
}
MyUserControl1.xaml
<UserControl
x:Class="App3.MyUserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App3"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
<Grid>
<StackPanel>
<TextBlock Text="Test" />
<TextBlock Text="Test" />
<TextBlock Text="Test" />
<Button Height="20"
Width="50"
Content="click"
Click="Button_Click"/>
<TextBlock Text="Test" />
<TextBlock Text="Test" />
<TextBlock Text="Test" />
</StackPanel>
</Grid>
</UserControl>
MyUserControl1.xaml.cs
namespace App3
{
public sealed partial class MyUserControl1 : UserControl
{
public MyUserControl1()
{
this.InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
//TODO : ?
}
}
}
我该怎么办?我不想在app.cs中使用属性......
答案 0 :(得分:3)
在Usercontrol中创建一个事件,如下所示:
namespace App3
{
public sealed partial class MyUserControl1 : UserControl
{
public event EventHandler ButtonClicked;
public MyUserControl1()
{
this.InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
//TODO : ?
if (ButtonClicked != null)
ButtonClicked(this, new EventArgs());
}
}
}
然后在你的XAML中为MainPage设置一个用户控件中的ButtonClicked事件的事件处理程序,如此
<uc:MyUserControl1 ButtonClicked="Increment_Click"></uc:MyUserControl1>
然后在代码Behind for MainPage中创建Increment_Click方法:
private void Increment_Click(Object sender, RoutedEventArgs e)
{
number++;
}
答案 1 :(得分:3)
在usercontrol中定义一个Dependency属性,并使用依赖属性将此属性绑定到MainPage.by中的一个属性,usercontrol就像一个独立的控件一样,就像一些Button Control等。它将给你一个如何使用DependencyProperty的例子太
在您的usercontrol中添加此代码
public sealed partial class MyUserControl1 : UserControl
{
public MyUserControl1()
{
this.InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
//TODO : ?
Caption = 2;
}
public int Caption
{
get { return (int)GetValue(CaptionProperty); }
set { SetValue(CaptionProperty, value); }
}
public static readonly DependencyProperty CaptionProperty =
DependencyProperty.Register("Caption", typeof(int), typeof(MyUserControl1),
new PropertyMetadata(0, null));
}
在你的MainPage.xaml中绑定这个标题属性。
<Grid>
<uc:MyUserControl1 Caption="{Binding SomeValue,Mode=TwoWay}" ></uc:MyUserControl1>
</Grid>
将属性添加到MainPage.Xaml.cs中。
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
this.DataContext = this;
}
public string SomeValue
{
set
{
string df = value; // put a break point here and check
}
}
}
Now put a break point on Set and see if is it changing or not.