Xamarin 形成如何修复 NavBar

Posted

技术标签:

【中文标题】Xamarin 形成如何修复 NavBar【英文标题】:Xamarin forms how to fix NavBar 【发布时间】:2022-01-13 10:38:40 【问题描述】:

Shell.NavBar 有问题,事实上当我打开编辑器时,NavBar 是不可见的。

我的代码页 xaml:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:Corporate_messenger.Service"
             x:Class="Corporate_messenger.Views.ChatPage" Shell.BackgroundColor="#04283C" Shell.TitleColor="White" x:Name="set_pqage">

    <ContentPage.ToolbarItems>
        <ToolbarItem  
            IconImageSource="audiosocket.png" 
            x:Name="VoiceRecord" 
            Clicked="VoiceRecord_Clicked">
        </ToolbarItem>
    </ContentPage.ToolbarItems>

    <Shell.BackButtonBehavior>
        <BackButtonBehavior 
            Command="Binding GoBack">
        </BackButtonBehavior>
    </Shell.BackButtonBehavior>

    <ContentPage.Resources>
        <ResourceDictionary>

            <DataTemplate 
            x:Key="FromTemplate">
                <ViewCell 
                x:Name="FromUserCell" 
                IsEnabled="True"  >
                    <ViewCell.View >
                        <FlexLayout 
                            JustifyContent="Start">

                            <Frame 
                            BackgroundColor="#ECEDF1"
                            CornerRadius="14"                          
                            Padding="5,10,10,10"
                            HasShadow="false"               
                            Margin="10,10,40,0">
                                <StackLayout 
                                    Orientation="Horizontal">
                                    <Label 
                                        Text="Binding Message" 
                                        IsEnabled="True"                                      
                                        HorizontalTextAlignment="Start"   
                                        TextColor="Black"/>
                                    <ImageButton 
                                        x:Name="LeftPlay" 
                                        Command="Binding Source=x:Reference MyListView,Path=BindingContext.PlayAudioMessage" 
                                        CommandParameter="Binding ."  
                                        Source="Binding Path=SourceImage,Mode=TwoWay"                                         
                                        IsVisible=" Binding IsAuidoVisible, Mode=TwoWay"       
                                        WidthRequest="35" HeightRequest="35" 
                                        CornerRadius="50" 
                                        BackgroundColor="#0986CE">
                                    </ImageButton>
                                    <Slider 
                                        x:Name="LeftSlider" 
                                        IsVisible="Binding IsAuidoVisible, Mode=TwoWay"
                                        Maximum="Binding MaximumSlider" 
                                        Value="Binding ValueSlider"
                                        IsEnabled="Binding IsEnableSlider" 
                                        MaximumTrackColor="Gray" 
                                        MinimumTrackColor="#41A9FE"                                         
                                        WidthRequest="150">
                                    </Slider>
                                    <Label   
                                        Text="Binding Time_LstMessage" 
                                        FontSize="11" MinimumWidthRequest="30" 
                                        VerticalTextAlignment="End" 
                                        HorizontalTextAlignment="End"   
                                        TextColor="DimGray"/>
                                </StackLayout>
                            </Frame>
                        </FlexLayout>
                    </ViewCell.View>
                </ViewCell>
            </DataTemplate>

            <!--Правый блок сообщений-->
            <DataTemplate 
                x:Key="ToTemplate">
                <ViewCell  
                x:Name="ToUserCell" 
                IsEnabled="True">
                    <FlexLayout 
                    JustifyContent="End" >
                        <Frame                      
                    CornerRadius="14" 
                    BackgroundColor="#CCE4FF"
                    Padding="5,10,10,10"
                    Margin="50,10,20,0"
                    HasShadow="false"      
                    IsClippedToBounds="True">
                            <StackLayout 
                            Orientation="Horizontal">
                                <Label    
                                    Text="Binding Message" 
                                    IsVisible="Binding IsMessageVisible, Mode=TwoWay"
                                    IsEnabled="True" 
                                    HorizontalTextAlignment="Start"   
                                    TextColor="Black"/>
                                <ImageButton
                                    x:Name="RightPlay"
                                    Command="Binding Source=x:Reference MyListView,Path=BindingContext.PlayAudioMessage" 
                                    CommandParameter="Binding ."  
                                    Source="Binding Path=SourceImage,Mode=TwoWay" 
                                    IsVisible=" Binding IsAuidoVisible, Mode=TwoWay" 
                                    WidthRequest="35" 
                                    HeightRequest="35" 
                                    CornerRadius="50" 
                                    BackgroundColor="#0986CE">
                                </ImageButton>

                                <Slider 
                                    Maximum="Binding MaximumSlider" 
                                    Value="Binding ValueSlider"                                    
                                    IsEnabled="Binding IsEnableSlider"                                    
                                    IsVisible="Binding IsAuidoVisible, Mode=TwoWay"
                                    WidthRequest="150"  
                                    MaximumTrackColor="Gray" 
                                    MinimumTrackColor="#41A9FE">
                                </Slider>

                                <Label  
                                    Text="Binding Time_LstMessage" 
                                    FontSize="11" 
                                    MinimumWidthRequest="30" 
                                    VerticalTextAlignment="End" 
                                    HorizontalTextAlignment="End"   
                                    TextColor="DimGray"/>
                            </StackLayout>
                        </Frame>
                    </FlexLayout>
                </ViewCell>
            </DataTemplate>
            <local:ClassDataTemplateSelector
                x:Key="chatDataTemplateSelector" 
                FromTemplate="StaticResource FromTemplate" 
                ToTemplate="StaticResource ToTemplate"/>
        </ResourceDictionary>
    </ContentPage.Resources>

    <ContentPage.Content>
        <StackLayout>
            
            <ListView
                IsRefreshing="Binding IsRefreshing" 
                RefreshCommand="Binding UpdateList"
                ItemTemplate="StaticResource chatDataTemplateSelector"  
                ItemsSource="Binding LastMessage"
                Background="#04283C" 
                IsPullToRefreshEnabled="True" 
                RefreshControlColor="Cyan"  
                Margin="0,0,0,0" 
                SeparatorColor="Transparent"  
                x:Name="MyListView" 
                HasUnevenRows="true" 
                >
            </ListView>

            <Grid 
                RowSpacing="0" 
                ColumnSpacing="0">

                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="auto" />
                    <ColumnDefinition Width="auto" />
                </Grid.ColumnDefinitions>
                 <!--Атрибут для написания сообщения-->
                <Editor
                    Text="Binding Input_message"  
                    Margin="5,0,0,0"  
                    x:Name="MessageEditor"  
                    Keyboard="Chat" 
                    TextColor="Black" 
                    Placeholder="Введите сообщение..." 
                    AutoSize="TextChanges" 
                    Grid.Row="0" 
                    Grid.Column="0">
                </Editor>
             
                <ImageButton
                    Command="Binding SendMessage"
                    CommandParameter="Binding"
                    Grid.Row="0" 
                    Grid.Column="1" 
                    Source="send_msg.png" 
                    BackgroundColor="Transparent" 
                    WidthRequest="32" HeightRequest="32" 
                    x:Name="send_message"  
                  />

                <ImageButton  
                    Pressed="mic_message_Pressed" 
                    Released="mic_message_Released" 
                    Grid.Row="0" 
                    Grid.Column="2" 
                    Source="mic.png" 
                    Padding="0" 
                    BackgroundColor="Transparent"  
                    WidthRequest="32" 
                    HeightRequest="32" 
                    x:Name="mic_message" 
                    Margin="0,0,10,0"/>
            </Grid>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

转到页面代码:

async void Handle_ItemTapped(object sender, ItemTappedEventArgs e)

    var v = (ChatListModel)e.Item;
    await Navigation.PushAsync(new ChatPage(v.Id, v.Title));

图片示例: enter image description here

enter image description here

【问题讨论】:

此页面是否包含在 NavigationPage 中? @Jason,我想是的 @Jason,我用 shell 导航 请在 xaml 或 cs 中显示代码 1) shell“路由”声明。 2) 转到该页面的 xaml 或 cs 代码行。去同步? @ToolmakerSteve,我在页面中添加了跳转代码,看看 【参考方案1】:

看起来像这样open issue - Shell - Navigation.PushAsync - Navigation Bar ignored on iOS。有一条评论说现在的解决方法是在每个页面上设置ios:Page.UseSafeArea="true"

<ContentPage
    ...
    xmlns:ios="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core"
    ios:Page.UseSafeArea="true"
    ... >

或者为了避免将其添加到每个页面,请创建一个包含这两行的 BaseContentPage 类,然后让您的页面继承自 BaseContentPage 而不是 ContentPage

【讨论】:

我正在写一个安卓应用

以上是关于Xamarin 形成如何修复 NavBar的主要内容,如果未能解决你的问题,请参考以下文章

Xamarin 为 UI 测试形成更改时间选择器值

Xamarin 使用水平堆栈布局形成面包屑

Xamarin 形成具有半径和背景颜色的网格

VS2019 Xamarin Forms Remote IOS模拟器黑屏 - 如何修复?

Xamarin 以编程方式在底部形成垂直对齐图像

如何修复“无法解决 xamarin android 中“System.Xml”之间的冲突?