在 VS 2010 Express for Windows Phone 中更改文本块 UserControl 上的文本

发布于 2025-01-08 01:06:41 字数 1352 浏览 0 评论 0原文

我在 VS2010 Express for Windows Phone 中创建了具有文本块的 UserControl,并将其添加到 MainPage.xaml 中。但是,我想在代码隐藏或 .xaml 文件中设置文本块上的文本。有人可以向我展示或给出示例或链接吗?提前致谢。

<UserControl x:Class="PhoneApp1.TitleControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
d:DesignHeight="480" d:DesignWidth="480">  

<Grid x:Name="LayoutRoot">
 <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,10,28">
  <TextBlock x:Name="ApplicationTitle"  Style="{StaticResource appTitleStyle}"/>
  <Grid  Height="45" Style="{StaticResource pageTitleBackgroudStyle}">
   <TextBlock x:Name="PageTitle"     Margin="9,-7,0,17" Style="{StaticResource pageTitleStyle}"/>
        </Grid>
    </StackPanel>
</Grid>

这是我的 MainPage.xaml:

<StackPanel x:Name="Titleqq" Grid.Row="0" Margin="12,17,0,28" Grid.ColumnSpan="2">
  <local:TitleControl x:name="Title" />
</StackPanel>         

I created the UserControl which has textblock in VS2010 Express for Windows Phone and added it on the MainPage.xaml. However, I would like to set the text on the the textblock either in codebehind or .xaml file. Would anyone show or give an example or link to me. Thank in advance.

<UserControl x:Class="PhoneApp1.TitleControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
d:DesignHeight="480" d:DesignWidth="480">  

<Grid x:Name="LayoutRoot">
 <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,10,28">
  <TextBlock x:Name="ApplicationTitle"  Style="{StaticResource appTitleStyle}"/>
  <Grid  Height="45" Style="{StaticResource pageTitleBackgroudStyle}">
   <TextBlock x:Name="PageTitle"     Margin="9,-7,0,17" Style="{StaticResource pageTitleStyle}"/>
        </Grid>
    </StackPanel>
</Grid>

This is my MainPage.xaml:

<StackPanel x:Name="Titleqq" Grid.Row="0" Margin="12,17,0,28" Grid.ColumnSpan="2">
  <local:TitleControl x:name="Title" />
</StackPanel>         

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

逆蝶 2025-01-15 01:06:41

编写用户控件时需要注意两件事。

  1. 编写一个在用户控件中定义属性的类。

  2. 将其绑定到您希望在 Windows Phone 应用程序中公开或设置的属性。

所以你的用户控件最好有,

UserControl.xaml
UserControl.xaml.cs

您在 cs 文件中编写类。将其绑定到您要在应用程序中设置的属性。所以在这种情况下,你会做这样的事情,

<UserControl x:Class="PhoneApp1.TitleControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
d:DesignHeight="480" d:DesignWidth="480"
x:Name="parent"
>  
<Grid x:Name="LayoutRoot"
      DataContext="{Binding ElementName=parent}">
 <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,10,28">
  <TextBlock x:Name="ApplicationTitle"  Style="{StaticResource appTitleStyle}"/>
  <Grid  Height="45" Style="{StaticResource pageTitleBackgroudStyle}">
   <TextBlock x:Name="PageTitle"  Text="{Binding Path=Text"}   Margin="9,-7,0,17" Style="{StaticResource pageTitleStyle}"/>
        </Grid>
    </StackPanel>
</Grid>

现在在你的代码后面编写一个继承自 UserControl 的类并绑定它......
Link1 和 google 上的 msdn Windows Phone UserControl 类。
点击链接->

There are two things to note when writing a usercontrol.

  1. Write a class that defines properties in your usercontrol.

  2. Bind it to the properties that you wish to expose or set in your windows phone application.

So your Usercontrol would Ideally have,

UserControl.xaml
UserControl.xaml.cs

You write your class in cs file. Bind it to the properties that you want to set in your application. So in this case you do something like this,

<UserControl x:Class="PhoneApp1.TitleControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
d:DesignHeight="480" d:DesignWidth="480"
x:Name="parent"
>  
<Grid x:Name="LayoutRoot"
      DataContext="{Binding ElementName=parent}">
 <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,10,28">
  <TextBlock x:Name="ApplicationTitle"  Style="{StaticResource appTitleStyle}"/>
  <Grid  Height="45" Style="{StaticResource pageTitleBackgroudStyle}">
   <TextBlock x:Name="PageTitle"  Text="{Binding Path=Text"}   Margin="9,-7,0,17" Style="{StaticResource pageTitleStyle}"/>
        </Grid>
    </StackPanel>
</Grid>

Now in your code behind write a class that inherits from UserControl and bind it...
Link1 and google on msdn Windows phone UserControl class.
Follow the link->

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文