我只是用谷歌搜索了很多帖子,但沒有找到有用的文章。我有一個具有 serval 模塊的 SmartEntry 應用程式。其中第一個儀表板頁面有導航按鈕和最后一個標題行,即標題 影像按鈕。
看一看
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="SmartEntry.DashBoard"
Title="Dashboard"
BackgroundColor="White">
<ContentPage.ToolbarItems>
<ToolbarItem Text="Dashboard" Order="Primary" Priority="0"></ToolbarItem>
<ToolbarItem Icon="logout_icon.png" Order="Primary" Priority="1" Clicked="ToolbarItem_Clicked"/>
</ContentPage.ToolbarItems>
<ContentPage.Content>
<StackLayout>
<Frame BackgroundColor="White" HasShadow="False">
<StackLayout Orientation="Vertical" Spacing="10" WidthRequest="100">
......
現在我只希望這個儀表板標題左對齊我是如何實作這一點的。螢屏:

如何讓儀表板向左對齊....
uj5u.com熱心網友回復:
如上所述,您可以使用 titleview,也可以為 NavigationPage 而不是 Page 創建自定義渲染器,并覆寫 OnLayout 方法。 相關檔案:https : //docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/custom-renderer/
Android 將在 UpdateToolbar 方法中更改 detault 圖示,并且每次更改當前頁面時都會觸發 OnLayout 方法。
在安卓上:
[assembly: ExportRenderer(typeof(NavigationPage), typeof(MyNavigationRenderer))]
namespace FormsApp.Droid
{
public class MyNavigationRenderer : NavigationPageRenderer
{
Context _context;
AndroidX.AppCompat.Widget.Toolbar _toolbar;
public MyNavigationRenderer(Context context) : base(context)
{
_context = context;
}
public override void OnViewAdded(Android.Views.View child)
{
base.OnViewAdded(child);
if (child.GetType() == typeof(AndroidX.AppCompat.Widget.Toolbar))
{
_toolbar = (AndroidX.AppCompat.Widget.Toolbar)child;
_toolbar.SetNavigationIcon(Resource.Drawable.bbutton_nav);
}
}
protected override void OnLayout(bool changed, int l, int t, int r, int b)
{
base.OnLayout(changed, l, t, r, b);
if (_toolbar != null)
{
if (_toolbar.NavigationIcon != null)
{
_toolbar.NavigationIcon = AndroidX.Core.Content.ContextCompat.GetDrawable(_context, Resource.Drawable.bbutton_nav);
}
}
}
}
}
在 Ios 上:
[assembly: ExportRenderer(typeof(NavigationPage), typeof(MyRenderer))]
namespace FormsApp.iOS
{
class MyRenderer : NavigationRenderer
{
public override void ViewDidLayoutSubviews()
{
base.ViewDidLayoutSubviews();
if (this.NavigationBar.TopItem.BackBarButtonItem == null)
{
this.NavigationBar.BackIndicatorImage = UIImage.FromFile("pic.png");
this.NavigationBar.BackIndicatorTransitionMaskImage = UIImage.FromFile("pic.png");
this.NavigationBar.TopItem.BackBarButtonItem = new UIBarButtonItem("Dashboard", UIBarButtonItemStyle.Plain, null);
}
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/365558.html
標籤:沙马林 xamarin.forms
上一篇:為什么這兩個影像在Firefox中顯示為不同的紅色陰影,如果在Photoshop中它們看起來相同并且都嵌入了sRGB組態檔?
