如何从 Xamarin 中 App class 中的全局变量绑定到 Codebehind
How to bind in Codebehind from a global variable in App class in Xamarin
我正在尝试创建一个带有传统购物车图标的 NavigationPage TitleView,上面写着 (3) Items 等。
所以全局 CartCount 变量在 App class 中声明。
我的 CommonToolbarPage class 在所有 50 个页面的顶部布置了徽标、搜索按钮和购物车图标,就像互联网上的每个商店一样。我正在使用棱镜。
我想要的是绑定到 CartCount 的内容,以便 BadgeText 在 CartCount 更新时更新。
BadgeText = App.CartCount , // Jiberish
减少class
public class CommonToolbarPage : ContentPage
{
public CommonToolbarPage()
{
//NavigationPage.SetHasBackButton(this, false);
ShowDefaultTitle();
//this.BindingContext= new CommonToolbarPageViewModel(navigationService);
}
private void ShowDefaultTitle()
{
SfButton sfButton = new SfButton
{
CornerRadius = 4,
HorizontalOptions = LayoutOptions.End,
VerticalOptions = LayoutOptions.Center,
Style = (Style)Application.Current.Resources["IconButtonStyle"],
Text = (String)Application.Current.Resources["Cart"],
FontFamily = "UIFontIcons",
TextColor = Color.White,
// Command = new Command(GoCart)
};
var Endlayout = new StackLayout
{
Orientation = StackOrientation.Horizontal,
HorizontalOptions = LayoutOptions.EndAndExpand,
};
var imSfBadgeView = new SfBadgeView
{
BadgeText = App.Cart, // Jiberish
Margin = new Thickness(0, 0, 10, 0),
Padding = new Thickness(0),
WidthRequest = 40,
// Content = sfButton,
HorizontalOptions = LayoutOptions.EndAndExpand,
VerticalOptions = LayoutOptions.Center,
BackgroundColor = Color.Transparent,
BadgeSettings = new BadgeSetting
{
BackgroundColor = (Color)Application.Current.Resources["PrimaryColor"],
BadgeType = BadgeType.None,
FontSize = 10,
Stroke = (Color)Application.Current.Resources["Gray-White"],
StrokeWidth = 1,
Offset = new Point(-10, 10)
}
};
Endlayout.Children.Add(imSfBadgeView);
NavigationPage.SetTitleView(this, Endlayout);
}
}
每个页面看起来像这样:
<?xml version="1.0" encoding="utf-8" ?>
<local1:CommonToolbarPage xmlns:local1="clr-namespace:blaAppV1.Views.Templates"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:listview="clr-namespace:Syncfusion.ListView.XForms;assembly=Syncfusion.SfListView.XForms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:prism="http://prismlibrary.com"
prism:ViewModelLocator.AutowireViewModel="True"
xmlns:local="clr-namespace:blaAppV1.Views;assembly=blaAppV1"
x:Class="blaAppV1.Views.Home">
<StackLayout VerticalOptions="StartAndExpand" HorizontalOptions="Center">
<Label Text="Home" VerticalOptions="Center" HorizontalOptions="Center" />
<Button Text="Appliances" Command="{Binding Path=NavigateCommand}" CommandParameter="Appliances" />
</StackLayout>
您使用 SetBinding
在代码中创建绑定
myLabel.SetBinding(Label.TextProperty, "CartCount");
myLabel.BindingContext = App;
我正在尝试创建一个带有传统购物车图标的 NavigationPage TitleView,上面写着 (3) Items 等。 所以全局 CartCount 变量在 App class 中声明。 我的 CommonToolbarPage class 在所有 50 个页面的顶部布置了徽标、搜索按钮和购物车图标,就像互联网上的每个商店一样。我正在使用棱镜。
我想要的是绑定到 CartCount 的内容,以便 BadgeText 在 CartCount 更新时更新。
BadgeText = App.CartCount , // Jiberish
减少class
public class CommonToolbarPage : ContentPage
{
public CommonToolbarPage()
{
//NavigationPage.SetHasBackButton(this, false);
ShowDefaultTitle();
//this.BindingContext= new CommonToolbarPageViewModel(navigationService);
}
private void ShowDefaultTitle()
{
SfButton sfButton = new SfButton
{
CornerRadius = 4,
HorizontalOptions = LayoutOptions.End,
VerticalOptions = LayoutOptions.Center,
Style = (Style)Application.Current.Resources["IconButtonStyle"],
Text = (String)Application.Current.Resources["Cart"],
FontFamily = "UIFontIcons",
TextColor = Color.White,
// Command = new Command(GoCart)
};
var Endlayout = new StackLayout
{
Orientation = StackOrientation.Horizontal,
HorizontalOptions = LayoutOptions.EndAndExpand,
};
var imSfBadgeView = new SfBadgeView
{
BadgeText = App.Cart, // Jiberish
Margin = new Thickness(0, 0, 10, 0),
Padding = new Thickness(0),
WidthRequest = 40,
// Content = sfButton,
HorizontalOptions = LayoutOptions.EndAndExpand,
VerticalOptions = LayoutOptions.Center,
BackgroundColor = Color.Transparent,
BadgeSettings = new BadgeSetting
{
BackgroundColor = (Color)Application.Current.Resources["PrimaryColor"],
BadgeType = BadgeType.None,
FontSize = 10,
Stroke = (Color)Application.Current.Resources["Gray-White"],
StrokeWidth = 1,
Offset = new Point(-10, 10)
}
};
Endlayout.Children.Add(imSfBadgeView);
NavigationPage.SetTitleView(this, Endlayout);
}
}
每个页面看起来像这样:
<?xml version="1.0" encoding="utf-8" ?>
<local1:CommonToolbarPage xmlns:local1="clr-namespace:blaAppV1.Views.Templates"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:listview="clr-namespace:Syncfusion.ListView.XForms;assembly=Syncfusion.SfListView.XForms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:prism="http://prismlibrary.com"
prism:ViewModelLocator.AutowireViewModel="True"
xmlns:local="clr-namespace:blaAppV1.Views;assembly=blaAppV1"
x:Class="blaAppV1.Views.Home">
<StackLayout VerticalOptions="StartAndExpand" HorizontalOptions="Center">
<Label Text="Home" VerticalOptions="Center" HorizontalOptions="Center" />
<Button Text="Appliances" Command="{Binding Path=NavigateCommand}" CommandParameter="Appliances" />
</StackLayout>
您使用 SetBinding
在代码中创建绑定
myLabel.SetBinding(Label.TextProperty, "CartCount");
myLabel.BindingContext = App;