maui-linux/Xamarin.Forms.Core/TableView.cs

250 строки
6.2 KiB
C#
Исходник Обычный вид История

2016-03-22 23:02:25 +03:00
using System;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using Xamarin.Forms.Platform;
using Xamarin.Forms.Internals;
2016-03-22 23:02:25 +03:00
namespace Xamarin.Forms
{
[ContentProperty("Root")]
[RenderWith(typeof(_TableViewRenderer))]
Platform Specifics (#301) * Playing around with how the platform specifics interfaces etc. might work * Sample implementation of iOS navigation translucency * Very slightly reduced code * Better vendor stuff * Drop single-implemenation interfaces * Generics on NavigationPage * On-demand vendor stuff * Remove functionally duplicate classes and make ControlGallery work again * Namespace all the things. XAML test. * Can use Effect to attach platform specific * Attach Effect on PropertyChanging for XAML support! * Rename IConfigPlatform interfaces for readability * Some renaming to match the documents * Split class files * Clear out test-only code * Re-namespace * Added On method to rendered Elements * Allow for removal of platform suffix, convenience methods on specific platforms * Creating a gallery page for specifics * Add rudimentary Platform Specifics gallery; make CollapseStyle work on UWP; Add CollapsedPaneWidth specific property * Toolbar now working with both collapse styles * MDP now displaying Content title; toolbar routing around title * Add a gallery for the iOS NavigationPage stuff * Add Navigation Page as detail page to verify it works with new Toolbar options * Make titlebar/toolbar background colors consistent * ToolbarPlacement now working on NavigationPage * Toolbar Placement working for tabbed and nav pages * Fix bug where phone doesn't get default toolbar placement on start * [Core] Add PS WindowSoftInputModeAdjust [Core] Make Application extendable * Toolbar placement now working on Nav, Tabbed, and Master pages on desktop/phone Remove unnecessary style indirection Fix build errors * [A] Add PlatformConfigurationExtensions * SetSoftInputMode test page * [A] SetSoftInputMode Known issue: Status bar color does not work in AdjustResize mode * [Core] Add PS Blur * [iOS] Configure renderer for blur * Add test page * Move to blur VisualElement for broader support * Move test pages to gallery * Update docs * Use lazy initializer for PlatformConfigurationRegistry
2016-08-30 20:46:14 +03:00
public class TableView : View, ITableViewController, IElementConfiguration<TableView>
2016-03-22 23:02:25 +03:00
{
public static readonly BindableProperty RowHeightProperty = BindableProperty.Create("RowHeight", typeof(int), typeof(TableView), -1);
public static readonly BindableProperty HasUnevenRowsProperty = BindableProperty.Create("HasUnevenRows", typeof(bool), typeof(TableView), false);
Platform Specifics (#301) * Playing around with how the platform specifics interfaces etc. might work * Sample implementation of iOS navigation translucency * Very slightly reduced code * Better vendor stuff * Drop single-implemenation interfaces * Generics on NavigationPage * On-demand vendor stuff * Remove functionally duplicate classes and make ControlGallery work again * Namespace all the things. XAML test. * Can use Effect to attach platform specific * Attach Effect on PropertyChanging for XAML support! * Rename IConfigPlatform interfaces for readability * Some renaming to match the documents * Split class files * Clear out test-only code * Re-namespace * Added On method to rendered Elements * Allow for removal of platform suffix, convenience methods on specific platforms * Creating a gallery page for specifics * Add rudimentary Platform Specifics gallery; make CollapseStyle work on UWP; Add CollapsedPaneWidth specific property * Toolbar now working with both collapse styles * MDP now displaying Content title; toolbar routing around title * Add a gallery for the iOS NavigationPage stuff * Add Navigation Page as detail page to verify it works with new Toolbar options * Make titlebar/toolbar background colors consistent * ToolbarPlacement now working on NavigationPage * Toolbar Placement working for tabbed and nav pages * Fix bug where phone doesn't get default toolbar placement on start * [Core] Add PS WindowSoftInputModeAdjust [Core] Make Application extendable * Toolbar placement now working on Nav, Tabbed, and Master pages on desktop/phone Remove unnecessary style indirection Fix build errors * [A] Add PlatformConfigurationExtensions * SetSoftInputMode test page * [A] SetSoftInputMode Known issue: Status bar color does not work in AdjustResize mode * [Core] Add PS Blur * [iOS] Configure renderer for blur * Add test page * Move to blur VisualElement for broader support * Move test pages to gallery * Update docs * Use lazy initializer for PlatformConfigurationRegistry
2016-08-30 20:46:14 +03:00
readonly Lazy<PlatformConfigurationRegistry<TableView>> _platformConfigurationRegistry;
2016-03-22 23:02:25 +03:00
readonly TableSectionModel _tableModel;
TableIntent _intent = TableIntent.Data;
TableModel _model;
public TableView() : this(null)
{
}
public TableView(TableRoot root)
{
VerticalOptions = HorizontalOptions = LayoutOptions.FillAndExpand;
Model = _tableModel = new TableSectionModel(this, root);
Platform Specifics (#301) * Playing around with how the platform specifics interfaces etc. might work * Sample implementation of iOS navigation translucency * Very slightly reduced code * Better vendor stuff * Drop single-implemenation interfaces * Generics on NavigationPage * On-demand vendor stuff * Remove functionally duplicate classes and make ControlGallery work again * Namespace all the things. XAML test. * Can use Effect to attach platform specific * Attach Effect on PropertyChanging for XAML support! * Rename IConfigPlatform interfaces for readability * Some renaming to match the documents * Split class files * Clear out test-only code * Re-namespace * Added On method to rendered Elements * Allow for removal of platform suffix, convenience methods on specific platforms * Creating a gallery page for specifics * Add rudimentary Platform Specifics gallery; make CollapseStyle work on UWP; Add CollapsedPaneWidth specific property * Toolbar now working with both collapse styles * MDP now displaying Content title; toolbar routing around title * Add a gallery for the iOS NavigationPage stuff * Add Navigation Page as detail page to verify it works with new Toolbar options * Make titlebar/toolbar background colors consistent * ToolbarPlacement now working on NavigationPage * Toolbar Placement working for tabbed and nav pages * Fix bug where phone doesn't get default toolbar placement on start * [Core] Add PS WindowSoftInputModeAdjust [Core] Make Application extendable * Toolbar placement now working on Nav, Tabbed, and Master pages on desktop/phone Remove unnecessary style indirection Fix build errors * [A] Add PlatformConfigurationExtensions * SetSoftInputMode test page * [A] SetSoftInputMode Known issue: Status bar color does not work in AdjustResize mode * [Core] Add PS Blur * [iOS] Configure renderer for blur * Add test page * Move to blur VisualElement for broader support * Move test pages to gallery * Update docs * Use lazy initializer for PlatformConfigurationRegistry
2016-08-30 20:46:14 +03:00
_platformConfigurationRegistry = new Lazy<PlatformConfigurationRegistry<TableView>>(() => new PlatformConfigurationRegistry<TableView>(this));
2016-03-22 23:02:25 +03:00
}
public bool HasUnevenRows
{
get { return (bool)GetValue(HasUnevenRowsProperty); }
set { SetValue(HasUnevenRowsProperty, value); }
}
public TableIntent Intent
{
get { return _intent; }
set
{
if (_intent == value)
return;
OnPropertyChanging();
_intent = value;
OnPropertyChanged();
}
}
public TableRoot Root
{
get { return _tableModel.Root; }
set
{
if (_tableModel.Root != null)
{
_tableModel.Root.SectionCollectionChanged -= OnSectionCollectionChanged;
_tableModel.Root.PropertyChanged -= OnTableModelRootPropertyChanged;
}
_tableModel.Root = value ?? new TableRoot();
SetInheritedBindingContext(_tableModel.Root, BindingContext);
Root.SelectMany(r => r).ForEach(cell => cell.Parent = this);
_tableModel.Root.SectionCollectionChanged += OnSectionCollectionChanged;
_tableModel.Root.PropertyChanged += OnTableModelRootPropertyChanged;
OnModelChanged();
}
}
public int RowHeight
{
get { return (int)GetValue(RowHeightProperty); }
set { SetValue(RowHeightProperty, value); }
}
[EditorBrowsable(EditorBrowsableState.Never)]
public TableModel Model
2016-03-22 23:02:25 +03:00
{
get { return _model; }
set
{
_model = value;
OnModelChanged();
}
}
ITableModel ITableViewController.Model
{
get
{
return Model;
}
}
2016-03-22 23:02:25 +03:00
protected override void OnBindingContextChanged()
{
base.OnBindingContextChanged();
if (Root != null)
SetInheritedBindingContext(Root, BindingContext);
}
protected virtual void OnModelChanged()
{
foreach (Cell cell in Root.SelectMany(r => r))
cell.Parent = this;
if (ModelChanged != null)
ModelChanged(this, EventArgs.Empty);
}
[Obsolete("Use OnMeasure")]
protected override SizeRequest OnSizeRequest(double widthConstraint, double heightConstraint)
{
var minimumSize = new Size(40, 40);
double width = Math.Min(Device.Info.ScaledScreenSize.Width, Device.Info.ScaledScreenSize.Height);
var request = new Size(width, Math.Max(Device.Info.ScaledScreenSize.Width, Device.Info.ScaledScreenSize.Height));
return new SizeRequest(request, minimumSize);
}
internal event EventHandler ModelChanged;
event EventHandler ITableViewController.ModelChanged
{
add { ModelChanged += value; }
remove { ModelChanged -= value; }
}
2016-03-22 23:02:25 +03:00
Platform Specifics (#301) * Playing around with how the platform specifics interfaces etc. might work * Sample implementation of iOS navigation translucency * Very slightly reduced code * Better vendor stuff * Drop single-implemenation interfaces * Generics on NavigationPage * On-demand vendor stuff * Remove functionally duplicate classes and make ControlGallery work again * Namespace all the things. XAML test. * Can use Effect to attach platform specific * Attach Effect on PropertyChanging for XAML support! * Rename IConfigPlatform interfaces for readability * Some renaming to match the documents * Split class files * Clear out test-only code * Re-namespace * Added On method to rendered Elements * Allow for removal of platform suffix, convenience methods on specific platforms * Creating a gallery page for specifics * Add rudimentary Platform Specifics gallery; make CollapseStyle work on UWP; Add CollapsedPaneWidth specific property * Toolbar now working with both collapse styles * MDP now displaying Content title; toolbar routing around title * Add a gallery for the iOS NavigationPage stuff * Add Navigation Page as detail page to verify it works with new Toolbar options * Make titlebar/toolbar background colors consistent * ToolbarPlacement now working on NavigationPage * Toolbar Placement working for tabbed and nav pages * Fix bug where phone doesn't get default toolbar placement on start * [Core] Add PS WindowSoftInputModeAdjust [Core] Make Application extendable * Toolbar placement now working on Nav, Tabbed, and Master pages on desktop/phone Remove unnecessary style indirection Fix build errors * [A] Add PlatformConfigurationExtensions * SetSoftInputMode test page * [A] SetSoftInputMode Known issue: Status bar color does not work in AdjustResize mode * [Core] Add PS Blur * [iOS] Configure renderer for blur * Add test page * Move to blur VisualElement for broader support * Move test pages to gallery * Update docs * Use lazy initializer for PlatformConfigurationRegistry
2016-08-30 20:46:14 +03:00
public IPlatformElementConfiguration<T, TableView> On<T>() where T : IConfigPlatform
{
return _platformConfigurationRegistry.Value.On<T>();
}
2016-03-22 23:02:25 +03:00
void CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
OnModelChanged();
}
void OnSectionCollectionChanged(object sender, ChildCollectionChangedEventArgs childCollectionChangedEventArgs)
{
if (childCollectionChangedEventArgs.Args.NewItems != null)
childCollectionChangedEventArgs.Args.NewItems.Cast<Cell>().ForEach(cell => cell.Parent = this);
OnModelChanged();
}
void OnTableModelRootPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == TableSectionBase.TitleProperty.PropertyName)
OnModelChanged();
}
internal class TableSectionModel : TableModel
{
static readonly BindableProperty PathProperty = BindableProperty.Create("Path", typeof(Tuple<int, int>), typeof(Cell), null);
readonly TableView _parent;
TableRoot _root;
public TableSectionModel(TableView tableParent, TableRoot tableRoot)
{
_parent = tableParent;
Root = tableRoot ?? new TableRoot();
}
public TableRoot Root
{
get { return _root; }
set
{
if (_root == value)
return;
RemoveEvents(_root);
_root = value;
ApplyEvents(_root);
}
}
public override Cell GetCell(int section, int row)
{
var cell = (Cell)GetItem(section, row);
SetPath(cell, new Tuple<int, int>(section, row));
return cell;
}
public override object GetItem(int section, int row)
{
return _root[section][row];
}
public override int GetRowCount(int section)
{
return _root[section].Count;
}
public override int GetSectionCount()
{
return _root.Count;
}
public override string GetSectionTitle(int section)
{
return _root[section].Title;
}
protected override void OnRowSelected(object item)
{
base.OnRowSelected(item);
((Cell)item).OnTapped();
}
internal static Tuple<int, int> GetPath(Cell item)
{
if (item == null)
throw new ArgumentNullException("item");
return (Tuple<int, int>)item.GetValue(PathProperty);
}
void ApplyEvents(TableRoot tableRoot)
{
tableRoot.CollectionChanged += _parent.CollectionChanged;
tableRoot.SectionCollectionChanged += _parent.OnSectionCollectionChanged;
}
void RemoveEvents(TableRoot tableRoot)
{
if (tableRoot == null)
return;
tableRoot.CollectionChanged -= _parent.CollectionChanged;
tableRoot.SectionCollectionChanged -= _parent.OnSectionCollectionChanged;
}
static void SetPath(Cell item, Tuple<int, int> index)
{
if (item == null)
return;
item.SetValue(PathProperty, index);
}
}
}
}