【问题标题】:Editor not supporting scrolling inside the listview for UWP Xamarin编辑器不支持在 UWP Xamarin 的列表视图内滚动
【发布时间】:2016-12-31 10:37:31
【问题描述】:

ListView中不支持滚动的编辑器

在 android 和 ios 中都支持滚动,但我遇到了 UWP 问题

 <ListView 
         x:Name="listProjects" 
         HasUnevenRows="true"
         SeparatorVisibility="None"
         ItemsSource="{Binding Feedbacks}"
         HorizontalOptions="FillAndExpand" 
         ItemTapped="Handle_ItemTapped">

        <ListView.ItemTemplate>
          <DataTemplate>
             <ViewCell>
            <StackLayout  Padding="15,15,15,15" BackgroundColor="Transparent">
               <Editor HeightRequest="50" FontSize="Small"/>
                </StackLayout>
             </ViewCell>
          </DataTemplate>
            </ListView.ItemTemplate>
 </ListView>

【问题讨论】:

    标签: windows xamarin uwp uwp-xaml


    【解决方案1】:

    在 android 和 ios 中都支持滚动,但我遇到了 UWP 问题

    是的,目前编辑器控件在 UWP 应用程序中不能作为 ViewVell 的子控件滚动,我看到您在 bugzilla 站点中创建了一个错误:https://bugzilla.xamarin.com/show_bug.cgi?id=43660

    这里的解决方法是自定义 ViewCell 并在每个平台上创建自定义渲染器。对于 UWP,使用 RichEditBox 控件或其他可滚动控件作为替代。

    请看文档:Customizing a ViewCell

    XAML:

    <ListView.ItemTemplate>
      <DataTemplate>
        <local:NativeCell Name="{Binding Name}" Category="{Binding Category}" />
      </DataTemplate>
    </ListView.ItemTemplate>
    

    NativeCell:

    public class NativeCell : ViewCell
    {
        public static readonly BindableProperty NameProperty =
          BindableProperty.Create("Name", typeof(string), typeof(NativeCell), "");
    
        public string Name
        {
            get { return (string)GetValue(NameProperty); }
            set { SetValue(NameProperty, value); }
        }
    
        public static readonly BindableProperty CategoryProperty =
          BindableProperty.Create("Category", typeof(string), typeof(NativeCell), "");
    
        public string Category
        {
            get { return (string)GetValue(CategoryProperty); }
            set { SetValue(CategoryProperty, value); }
        }
    }
    

    为 UWP 定制的渲染:

    using WorkingWithListview;
    using WorkingWithListview.UWP.Renderers;
    using Xamarin.Forms;
    using Xamarin.Forms.Platform.UWP;
    
    [assembly: ExportRenderer(typeof(NativeCell), typeof(NativeUWPCellRenderer))]
    namespace WorkingWithListview.UWP.Renderers
    {
        public class NativeUWPCellRenderer : ViewCellRenderer
        {
            public override Windows.UI.Xaml.DataTemplate GetTemplate(Cell cell)
            {
                return App.Current.Resources["ListViewItemTemplate"] as Windows.UI.Xaml.DataTemplate;
            }
        }
    }
    

    App.xaml 中的数据模板:

    <DataTemplate x:Key="ListViewItemTemplate">
            <Grid Background="LightBlue">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="100" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="0.40*" />
                    <ColumnDefinition Width="0.40*"/>
                    <ColumnDefinition Width="0.20*" />
                </Grid.ColumnDefinitions>
                <RichEditBox Grid.Row="1" />
                <TextBlock Grid.ColumnSpan="2" Foreground="#7F3300" FontStyle="Italic" FontSize="22" VerticalAlignment="Top" Text="{Binding Name}" />
                <Line Grid.Row="2" Grid.ColumnSpan="3" X1="0" X2="1" Margin="30,20,0,0" StrokeThickness="1" Stroke="LightGray" Stretch="Fill" VerticalAlignment="Bottom" />
            </Grid>
        </DataTemplate>
    

    截图:

    检查我完成的样本here

    【讨论】:

    • 谢谢,富兰克林,它的工作,但在这里我还需要为编辑器设置绑定上下文,但 RichEditBox 不支持 Text 属性。
    • @SachinKs 很高兴知道,这是您问题的可接受答案吗? ;)
    • 是的,这是可以接受的,但在这里我还需要将绑定上下文设置为 Text 属性以在 RichEditBox 中设置默认文本,有什么办法吗?
    • @SachinKs 当然,但是这个问题与原来的问题无关,基于stackoverflow规则,请提出一个新问题,我会在那里给你建议:)(你可以告诉我这里的链接)
    猜你喜欢
    • 2014-08-12
    • 1970-01-01
    • 2014-08-20
    • 1970-01-01
    • 1970-01-01
    • 2016-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多