在Windows Phone中,从MainPage.xaml.cs文件到MainPage.xaml文件获取字符串Vari

本文关键字:文件 xaml MainPage 获取 字符串 Vari Windows cs Phone | 更新日期: 2023-09-27 17:59:27

我创建了一个项目,其中MainPage.xaml.cs如下所示:

using System;
using System.Collections.Generic;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
namespace DemoApp
{
    public partial class MainPage : PhoneApplicationPage
    {
        public string ImagePath = "/Images/Flower.png";
        public StartPage()
        {
            InitializeComponent();
        }
    }
}

我创建了MainPage.xaml,如下所示:

<phone:PhoneApplicationPage
    x:Class="DemoApp.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"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    mc:Ignorable="d"
    shell:SystemTray.IsVisible="True">      
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <Image x:Name="ImageShow" Source=""/>   
    </Grid>
</phone:PhoneApplicationPage>

现在我的问题是,有没有任何方法可以获得名为ImageShow as Source的图像的ImagePath(字符串)的值?有没有什么方法可以在不创建任何新类的情况下从MainPage.xaml和在这种情况下使用DataBinding来实现这一点?

在Windows Phone中,从MainPage.xaml.cs文件到MainPage.xaml文件获取字符串Vari

您需要将ImagePath转换为属性,因为您无法绑定到字段

public partial class MainPage : PhoneApplicationPage
{
    public string ImagePath { get; set; } 
    public MainPage()
    {
        ImagePath = "/Images/Flower.png";
        InitializeComponent();
    }
}

为页面指定一些名称并将Image.Source绑定到ImagePath属性

<phone:PhoneApplicationPage ... x:Name="myPage">          
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <Image x:Name="ImageShow" Source="{Binding ElementName=myPage, Path=ImagePath}"/>   
    </Grid>
</phone:PhoneApplicationPage>

在代码背后中使用DataContext

public string ImagePath = "/Images/Flower.png";
    public StartPage()
    {
        InitializeComponent();
        this.DataContext=this;
    }

然后你在你的.xaml中使用你绑定的

<Image x:Name="ImageShow" Source="{Binding ImagePath}"/>   

您应该创建DependencyProperty

public string ImagePath
{
    get { return (string) GetValue(ImagePathProperty); }
    set { SetValue(ImagePathProperty, value); }
}
public static readonly DependencyProperty ImagePathProperty =
        DependencyProperty.Register("ImagePath", typeof(string), typeof(MainPage), new PropertyMetadata(""));

这将创建可用于绑定的属性,之后您可以在Image:中绑定到它

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
    <Image x:Name="ImageShow" Source="{Binding Path=ImagePath, RelativeSource={RelativeSource AncestorType=PhoneApplicationPage}}"/>   
</Grid>

将您的变量添加为codeehind:中的资源

myWindow.Resources.Add("ImagePath", "/Images/Flower.png");
myWindow.DataContext = ImagePath;

XAML中的访问:

<Image x:Name="ImageShow" Source={Binding Path=ImagePath}"/>