【发布时间】:2016-11-18 08:49:57
【问题描述】:
美好的一天。我正在创建一个简单的 Xamarin.Forms (Portable) 程序。我只是在想我是否可以以某种方式“合并”或者将我的 XAML 文件中的标签调用到我的 XAML.cs。可能吗?因为每次我在 XAML.cs 中编写代码时,我在 XAML 文件中的现有代码似乎都不会出现。
例如,我的 SalesPage.xaml.cs 中有这段代码
public partial class SalesPage : ContentPage
{
Label resultsLabel;
SearchBar searchBar;
public SalesPage()
{
resultsLabel = new Label
{
Text = "Result will appear here.",
VerticalOptions = LayoutOptions.FillAndExpand,
FontSize = 25
};
searchBar = new SearchBar
{
Placeholder = "Enter search term",
SearchCommand = new Command(() => { resultsLabel.Text = "Result: " + searchBar.Text + " is what you asked for."; })
};
Content = new StackLayout
{
VerticalOptions = LayoutOptions.Start,
Children = {
new Label {
HorizontalTextAlignment = TextAlignment.Center,
Text = "SearchBar",
FontSize = 50,
TextColor = Color.Purple
},
searchBar,
resultsLabel,
new Label {
HorizontalTextAlignment = TextAlignment.Center,
Text = "SearchBar",
FontSize = 50,
TextColor = Color.Purple
},
new ScrollView
{
Content = resultsLabel,
VerticalOptions = LayoutOptions.FillAndExpand
}
},
Padding = new Thickness(10, Device.OnPlatform(20, 0, 0), 10, 5)
};
}
唯一会出现在屏幕上的是我在 Xaml.cs 上编写的代码。
看看这个。
我的 XAML 文件中有这段代码:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="XamarinFormsDemo.Views.SalesPage"
BackgroundImage="bg3.jpg"
Title="Sales Page">
<Label x:Name="SalesLabel"
VerticalOptions="Center"
HorizontalOptions="Center" />
</ContentPage>
如何在屏幕上显示我用 XAML.CS 编写的代码和我用 XAML 编写的代码?如何将它们一起显示?
非常感谢。非常感谢您的帮助。
【问题讨论】:
-
您的问题是,当您运行您的应用程序时,您只会看到 .xaml.cs 文件中编写的代码和 .xaml 文件中的代码,对吗?
-
@M.Pipal 我只看到写在我的 Xaml.cs 文件中的代码,而不是 .Xaml 文件中的那个。先生。
-
您能否再添加一点代码,在 SalesPage.xaml.cs 中的“内容”之前?它是哪个元素的内容属性?
-
@M.Pipal 好的,先生。我已经添加了代码。
标签: c# xaml xamarin xamarin.forms