簡體   English   中英

在Windows Phone中從MainPage.xaml.cs文件到MainPage.xaml文件獲取字符串變量的值

[英]Get value of a string Variable from MainPage.xaml.cs file to MainPage.xaml file in Windows Phone

我創建了一個項目,其中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>

現在我的問題是,有什么方法可以將ImagePath(字符串)的值獲取到名為ImageShow的Image作為Source? 有什么辦法可以從MainPage.xaml進行操作,並且在這種情況下使用DataBinding而不創建任何新類嗎?

在代碼中使用DataContext

public string ImagePath = "/Images/Flower.png";

    public StartPage()
    {
        InitializeComponent();
        this.DataContext=this;

    }

然后在.xaml中使用綁定

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

您需要將ImagePath轉換為屬性,因為您無法綁定到字段

public partial class MainPage : PhoneApplicationPage
{
    public string ImagePath { get; set; } 

    public MainPage()
    {
        ImagePath = "/Images/Flower.png";
        InitializeComponent();
    }
}

給頁面Image.Source ,然后將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>

您應該創建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>

從代碼后面將變量添加為資源:

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

myWindow.DataContext = ImagePath;

在XAML中訪問:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM