常用代码(待更新)

Model

public class Model : INotifyPropertyChanged
{
    private string content;
    public string Content {

        get
        {
            return content;
        }
        set {
            content = value;
            NotifyPropertyChanged();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

Custom Renderer

[assembly: ExportRenderer(typeof(TableView), typeof(CustomListRenderer))]
namespace PPPPCL.iOS
{
    class CustomListRenderer  :TableViewRenderer
    {
       protected override void OnElementChanged(ElementChangedEventArgs e)
        {
            base.OnElementChanged(e);
            if(Control != null)
            {
            
            }
        }
    }
}

BindableStackLayout

public class BindableStackLayout : StackLayout
{
    public IEnumerable ItemsSource
    {
        get { return (IEnumerable)GetValue(ItemsSourceProperty); }
        set { SetValue(ItemsSourceProperty, value); }
    }
    public static readonly BindableProperty ItemsSourceProperty =
        BindableProperty.Create(nameof(ItemsSource), typeof(IEnumerable), typeof(BindableStackLayout),
                                propertyChanged: (bindable, oldValue, newValue) => ((BindableStackLayout)bindable).PopulateItems());

    public DataTemplate ItemDataTemplate
    {
        get { return (DataTemplate)GetValue(ItemDataTemplateProperty); }
        set { SetValue(ItemDataTemplateProperty, value); }
    }
    public static readonly BindableProperty ItemDataTemplateProperty =
        BindableProperty.Create(nameof(ItemDataTemplate), typeof(DataTemplate), typeof(BindableStackLayout));

    void PopulateItems()
    {
        if (ItemsSource == null) return;
        foreach (var item in ItemsSource)
        {
            var itemTemplate = ItemDataTemplate.CreateContent() as Xamarin.Forms.View;
            itemTemplate.BindingContext = item;
            Children.Add(itemTemplate);
        }
    }
}

查看已有的fontName

foreach (var familyNames in UIFont.FamilyNames.OrderBy(c => c).ToList())
        {
            Console.WriteLine(" * " + familyNames);
            foreach (var familyName in UIFont.FontNamesForFamilyName(familyNames).OrderBy(c => c).ToList())
            {
                Console.WriteLine(" *-- " + familyName);
            }
        }

OnPlatform 用法

代码

 Icon = (Device.RuntimePlatform == Device.iOS) ? "tab_about.png" : null;

xaml

         
    
        
            
            
            
        
    
   

你可能感兴趣的:(常用代码(待更新))