从另一个类修改XAML TextBox

本文关键字:XAML TextBox 修改 另一个 | 更新日期: 2023-09-27 18:10:12

在我的MainPage PanoramaItem2中,我有一个TextBlock。如何从另一个类设置TextBlock的文本?

我尝试创建MainPage的实例,然后找到TextBlock,但我找不到它。

这是我尝试的方法:

public async Task<Problem> DownloadFileFromWeb(Uri uriToDownload, string fileName, CancellationToken cToken)
        {
            var MainText = new MainPage();
            //Set text here;
            //MainText.Panorama2.DLText.Text = "New text";
        }

这是我的MainPage的xaml:

<phone:PhoneApplicationPage
x:Class="PanoramaApp2.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DataContext="{d:DesignData SampleData/MainViewModelSampleData.xaml}"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait"  Orientation="Portrait"
shell:SystemTray.IsVisible="False">
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
    <!-- LOCALIZATION NOTE:
        To localize the displayed strings copy their values to appropriately named
        keys in the app's neutral language resource file (AppResources.resx) then
        replace the hard-coded text value between the attributes' quotation marks
        with the binding clause whose path points to that string name.
        For example:
            Text="{Binding Path=LocalizedResources.ApplicationTitle, Source={StaticResource LocalizedStrings}}"
        This binding points to the template's string resource named "ApplicationTitle".
        Adding supported languages in the Project Properties tab will create a
        new resx file per language that can carry the translated values of your
        UI strings. The binding in these examples will cause the value of the
        attributes to be drawn from the .resx file that matches the
        CurrentUICulture of the app at run time.
     -->
     <!--Panorama control-->
    <phone:Panorama Title="my application">
        <phone:Panorama.Background>
            <ImageBrush ImageSource="/PanoramaApp2;component/Assets/PanoramaBackground.png"/>
        </phone:Panorama.Background>
        <!--Panorama item one-->
        <phone:PanoramaItem x:Name="Panorama1" Header="first item">
            <!--Single line list with text wrapping-->
            <phone:LongListSelector Margin="0,0,-22,0" ItemsSource="{Binding Items}">
                <phone:LongListSelector.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Margin="0,-6,0,12">
                            <TextBlock Text="{Binding LineOne}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}" FontSize="{StaticResource PhoneFontSizeExtraLarge}"/>
                        </StackPanel>
                    </DataTemplate>
                </phone:LongListSelector.ItemTemplate>
            </phone:LongListSelector>
        </phone:PanoramaItem>
        <!--Panorama item two-->
        <phone:PanoramaItem x:Name="Panorama2" Header="Songs">
            <!--Double line list with image placeholder and text wrapping using a floating header that scrolls with the content-->
            <phone:LongListSelector Margin="0,-38,-22,2" ItemsSource="{Binding Items}" Tap="LongListSelector_Tap">
                <phone:LongListSelector.ListHeaderTemplate>
                    <DataTemplate>
                        <Grid Margin="12,0,0,38">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="*"/>
                            </Grid.RowDefinitions>
                            <TextBlock Name="DLText" Text="{Binding StatusText, Mode = TwoWay}"
                                       Style="{StaticResource PanoramaItemHeaderTextStyle}"
                                       Grid.Row="0"/>
                        </Grid>
                    </DataTemplate>
                </phone:LongListSelector.ListHeaderTemplate>
                <phone:LongListSelector.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal" Margin="12,2,0,4" Height="105" Width="432">
                            <StackPanel Width="311" Margin="8,-7,0,0">
                                <TextBlock Text="{Binding LineOne}" TextWrapping="Wrap" Margin="10,0" Style="{StaticResource PhoneTextExtraLargeStyle}" FontSize="{StaticResource PhoneFontSizeLarge}"/>
                                <TextBlock Text="{Binding LineTwo}" TextWrapping="Wrap" Margin="10,-2,10,0" Style="{StaticResource PhoneTextSubtleStyle}" />
                            </StackPanel>
                            <Image Source="{Binding PlayPhoto}" Width="50" Height="50" HorizontalAlignment="Left" Tap="Image_Tap_1"/>
                            <Image Source="{Binding DownloadPhoto}" Width="40" Height="40" HorizontalAlignment="Right" Tap="Image_Tap"/>
                        </StackPanel>
                    </DataTemplate>
                </phone:LongListSelector.ItemTemplate>
            </phone:LongListSelector>
        </phone:PanoramaItem>
        <!--Panorama item three-->
        <phone:PanoramaItem Header="third item" Orientation="Horizontal">
            <!--Double wide Panorama with large image placeholders-->
            <Grid>
                <StackPanel Margin="0,4,16,0" Orientation="Vertical" VerticalAlignment="Top">
                    <StackPanel HorizontalAlignment="Left" Orientation="Horizontal">
                        <Border Background="#FFFFC700" Height="173" Width="173" Margin="12,0,0,0"/>
                        <Border Background="#FFFFC700" Height="173" Width="173" Margin="12,0,0,0"/>
                        <Border Background="#FFFFC700" Height="173" Width="173" Margin="12,0,0,0"/>
                        <Border Background="#FFFFC700" Height="173" Width="173" Margin="12,0,0,0"/>
                    </StackPanel>
                    <StackPanel HorizontalAlignment="Left" Orientation="Horizontal" Margin="0,12,0,0">
                        <Border Background="#FFFFC700" Height="173" Width="173" Margin="12,0,0,0"/>
                        <Border Background="#FFFFC700" Height="173" Width="173" Margin="12,0,0,0"/>
                        <Border Background="#FFFFC700" Height="173" Width="173" Margin="12,0,0,0"/>
                        <Border Background="#FFFFC700" Height="173" Width="173" Margin="12,0,0,0"/>
                    </StackPanel>
                </StackPanel>
            </Grid>
        </phone:PanoramaItem>
    </phone:Panorama>
    <!--Uncomment to see an alignment grid to help ensure your controls are
        aligned on common boundaries.  The image has a top margin of -32px to
        account for the System Tray. Set this to 0 (or remove the margin altogether)
        if the System Tray is hidden.
        Before shipping remove this XAML and the image itself.-->
    <!--<Image Source="/Assets/AlignmentGrid.png" VerticalAlignment="Top" Height="800" Width="480" Margin="0" Grid.Row="0" Grid.RowSpan="2" IsHitTestVisible="False" />-->
</Grid>

我在这里错过了什么?我在WP8.1

从另一个类修改XAML TextBox

无论如何都不能这样修改文本,因为它在模板中。您应该使用绑定来设置文本:

<TextBlock Name="DLText" Text="{Binding StatusText,Mode=TwoWay}"
           Style="{StaticResource PanoramaItemHeaderTextStyle}"
           Grid.Row="0"/>

将"StatusText"属性添加到包含"Items"的视图模型中。

那么它只是一个更新视图模型属性的问题。如果"MainPage"在当前帧中,那么您可以使用如下内容:

// WinRT
// var frame = (Frame)Windows.UI.Xaml.Window.Current.Content;
// var page = (MainPage)frame.Content;
// Silverlight
var page = (MainPage)Application.Current.RootVisual;
var viewmodel = (MyViewModel)page.DataContext;
viewmodel.StatusText = "new text";