我有TableView
几个部分.在我TableSection
我想根据平台显示不同的控件.我想使用通常TextCell
的iOS和ViewCell
Android 的自定义.我试过以下代码:
<TableSection Title="Activity">
<OnPlatform x:TypeArguments="View">
<On Platform="iOS">
<TextCell x:Name="btnCountIOS" Text="Count" Height="50" />
<TextCell x:Name="cardHistory" Text="History" StyleId="disclosure" Tapped="openHistoryPage" Height="50"/>
</On>
<On Platform="Android">
<local:MyViewCell NoTap="true" Height="50">
<Grid VerticalOptions="CenterAndExpand" Padding="20, 0">
<Label Style="{DynamicResource ListItemTextStyle}" HorizontalOptions="StartAndExpand" VerticalOptions="Center" Text="Today" />
<Label Style="{DynamicResource ListItemTextStyle}" TextColor="Gray" HorizontalOptions="End" x:Name="btnCountDroid" />
</Grid>
</local:MyViewCell>
<local:MyViewCell>
<!-- more code here -->
</local:MyViewCell>
</On>
</OnPlatform>
</TableSection>
Run Code Online (Sandbox Code Playgroud)
上面的代码不起作用.它给了我一个错误:Object reference not set to an instance of an object
.
我错过了什么吗?我应该使用不同的x:TypeArguments
吗?
编辑:
编辑纳入@Fahadsk答案,现在得到错误: …
我有一个Xamarin Forms应用程序,它为用户提供了3个主题选项.我希望能够通过单击按钮更改Tabbar背景,所选项目和未选择的项目颜色.在iOS中,我可以使用如下的渲染器执行此操作:
protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
base.OnElementChanged(e);
if(e.OldElement != null)
{
Xamarin.Forms.Application.Current.PropertyChange -= Current_PropertyChanged;
return;
}
Xamarin.Forms.Application.Current.PropertyChange += Current_PropertyChanged; //subscribe to the App class' built in property changed event
UpdateTheme();
}
void Current_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
UpdateTheme();
}
Run Code Online (Sandbox Code Playgroud)
在Android中,我知道可以更改这些颜色,styles.xml
但这只允许我设置一次颜色.此外,我正在使用ToolbarPlacement="Bottom"
我的标签栏在屏幕的底部.
android:TabbedPage.ToolbarPlacement="Bottom"
android:TabbedPage.BarSelectedItemColor="Red"
android:TabbedPage.IsSwipePagingEnabled="False"
Run Code Online (Sandbox Code Playgroud)
我想知道是否有可能通过BarSelectedItemColor
点击按钮动态更改.
我有一个XF应用程序,TabbedPage
当用户处于测验模式时隐藏菜单,并在用户停止测验模式时再次显示该菜单.以下是我隐藏和显示菜单的代码:
public static void ClearNav()
{
navHomeTabPage.Icon = "";
navHomeTabPage.Title = "";
// more items
}
public static void SetNav()
{
navHomeTabPage.Icon = "home.png";
navHomeTabPage.Title = "Home";
// more items
}
Run Code Online (Sandbox Code Playgroud)
有了这个,我可以隐藏菜单项,但它们仍然是可点击的.有没有办法在Xamarin.Android中禁用此点击事件?我找不到任何关于此的文章/帖子.任何帮助表示赞赏.
编辑:
我BottomNavigationView
用来将我的标签菜单放在底部.
public class BottomTabPageRenderer : TabbedPageRenderer, BottomNavigationView.IOnNavigationItemSelectedListener, BottomNavigationView.IOnNavigationItemReselectedListener
{
private MainPage _page;
protected override void OnElementChanged(ElementChangedEventArgs<TabbedPage> e)
{
base.OnElementChanged(e);
if (e.NewElement != null)
{
_page = (MainPage)e.NewElement;
}
else
{
_page = (MainPage)e.OldElement;
}
// More codes
}
bool BottomNavigationView.IOnNavigationItemSelectedListener.OnNavigationItemSelected(IMenuItem item)
{
base.OnNavigationItemSelected(item); …
Run Code Online (Sandbox Code Playgroud) 我有一个Xamarin.Forms应用程序,该应用程序仅针对Android使用自定义导航标题视图。现在,我的标题视图已在我的模板构造函数中定义,如下所示:
[ContentProperty(nameof(InnerContent))]
public partial class ContentCustomTitleView : ContentPage
{
public static readonly BindableProperty InnerContentProperty = BindableProperty.Create(nameof(InnerContent), typeof(View), typeof(ContentCustomTitleView));
public static readonly BindableProperty PageTitleProperty = BindableProperty.Create(nameof(PageTitle), typeof(string), typeof(Label), default(string), BindingMode.OneWay);
public View InnerContent
{
get => (View)this.GetValue(InnerContentProperty);
set => this.SetValue(InnerContentProperty, value);
}
public string PageTitle
{
get
{
var value = (string)GetValue(PageTitleProperty);
return value;
}
set => SetValue(PageTitleProperty, value);
}
public ContentCustomTitleView()
{
InitializeComponent();
BindingContext = this;
if (Device.RuntimePlatform == "Android")
{
NavigationPage.SetHasBackButton(this, false);
NavigationPage.SetTitleView(this, SetBackView());
}
}
StackLayout SetBackView()
{ …
Run Code Online (Sandbox Code Playgroud) 我有一个Xamarin.Forms
应用程序,我创建了一个自定义Switch
如下:
public class ExtSwitch : Switch
{
public static readonly BindableProperty SwitchOnColorProperty =
BindableProperty.Create(nameof(SwitchOnColor),
typeof(Color), typeof(ExtSwitch), Color.Default);
public Color SwitchOnColor
{
get { return (Color)GetValue(SwitchOnColorProperty); }
set { SetValue(SwitchOnColorProperty, value); }
}
// More codes here //
}
Run Code Online (Sandbox Code Playgroud)
在 MyXAML
我使用它:
<local:ExtSwitch Grid.Column = "2"
IsToggled="{Binding IsToggled}"
Toggled="Handle_Toggled"
SwitchThumbColor="White"
SwitchOnColor="Red"
SwitchOffColor="Gray"
HorizontalOptions="End"
VerticalOptions="Center" />
Run Code Online (Sandbox Code Playgroud)
在我的 C# 代码中,我有一个Handle_Toggled
方法可以处理切换 Swich 时发生的情况。但不知何故,Toggled
在我的自定义开关中使用时不会触发事件,但在普通开关中使用时效果很好。
有人可以指出我在这里缺少什么或我做错了什么吗?
编辑:
iOS 中的自定义渲染器:
class ExtSwitchRenderer : SwitchRenderer
{
protected override void …
Run Code Online (Sandbox Code Playgroud) 我有一个 XF 应用程序。在我的 ViewModel 中,我有以下内容:
public ICommand ActBtnCmd { get; }
public ICommand AdpBtnCmd { get; }
public SettingsTabViewModel(SettingsTabPage settingsTabPage)
{
ActBtnCmd = new Command<Templates.Button>((btn) => MessagingCenter.Send(this, "ActBtn", btn));
AdpBtnCmd = new Command<Templates.Button>((btn) => MessagingCenter.Send(this, "AdpBtn", btn));
}
Run Code Online (Sandbox Code Playgroud)
而在我的XAML
:
<t:Button Text="{Binding ActBtnText}"
TapCommand="{Binding ActBtnCmd}"
WidthRequest="30"
Theme="{Binding Theme}" />
Run Code Online (Sandbox Code Playgroud)
在iOS中调试没有问题。但是当我在 Android 中调试应用程序时,我在应用程序输出窗口中收到这些消息:
Binding: 'ActBtnCmd' property not found on 'xxx.SettingsTabViewModel', target property: 'xxx.Templates.Button.TapCommand'
Binding: 'AdpBtnCmd' property not found on 'xxx.SettingsTabViewModel', target property: 'xxx.Templates.Button.TapCommand'
Run Code Online (Sandbox Code Playgroud)
但是当我像下面这样更改我的属性时,消息就消失了:
public ICommand ActBtnCmd { get; set; …
Run Code Online (Sandbox Code Playgroud) 我在 Xamarin Forms 中有以下代码:
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:XXX;assembly=XXX"
xmlns:android="clr-namespace:Xamarin.Forms.PlatformConfiguration.AndroidSpecific;assembly=Xamarin.Forms.Core"
BackgroundColor="{DynamicResource BarBackgroundColor}"
android:TabbedPage.ToolbarPlacement="Bottom"
android:TabbedPage.BarItemColor="Gray"
android:TabbedPage.BarSelectedItemColor="{DynamicResource BarSelectedItemColor}"
android:TabbedPage.IsSwipePagingEnabled="False"
x:Class="XXX.MainPage">
</TabbedPage>
Run Code Online (Sandbox Code Playgroud)
我想更改Android
侧面Tabbar 文本的大小。我尝试创建自己的风格,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyTabLayoutStyle" parent="TextAppearance.Design.Tab">
<item name="android:textSize">5sp</item>
</style>
</resources>
Run Code Online (Sandbox Code Playgroud)
而在我的Tabbar.axml
:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.TabLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/tabs"
android:layout_width="match_parent"
app:tabGravity="fill"
app:tabMode="fixed"
app:tabTextAppearance="@style/MyTabLayoutStyle"/>
Run Code Online (Sandbox Code Playgroud)
我有一种感觉这不起作用,因为我正在使用TabbedPage.ToolbarPlacement="Bottom"
而不是使用TabLayout
,我现在正在使用BottomNavigationView
. 因此,上面的问题,如何在使用TabbedPage.ToolbarPlacement="Bottom"
.