![](/img/trans.png)
[英]Get value of an int Variable from MainPage.xaml.cs file to MainPage.xaml file in Xamarin
[英]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.