コメント
コメントの投稿
※名前とタイトルが入力されていないコメントでは他のコメントとの区別ができません。
入力されていないコメントには返信しませんのであらかじめご了承くださいませ。
using Xamarin.Forms;
namespace AppName.Controls
{
public class BaseLinkButton : Label
{
public BaseLinkButton() : base()
{
//初期値
this.IsUnderline = true;
this.IsDisabled = false;
base.BackgroundColor = Color.Transparent;
//リンクをタップした際の動作を設定
var tgr = new TapGestureRecognizer();
tgr.Tapped += this.OnClicked;
this.GestureRecognizers.Add(tgr);
}
public static BindableProperty UrlProperty = BindableProperty.Create(
propertyName: "Url",
returnType: typeof(string),
declaringType: typeof(BaseLinkButton),
defaultValue: "",
defaultBindingMode: BindingMode.TwoWay);
public static BindableProperty IsUnderlineProperty = BindableProperty.Create(
propertyName: "IsUnderline",
returnType: typeof(bool),
declaringType: typeof(BaseLinkButton),
defaultValue: true,
defaultBindingMode: BindingMode.TwoWay);
//URLを設定するプロパティ
public string Url
{
get
{
return (string)GetValue(UrlProperty); ;
}
set
{
SetValue(UrlProperty, value);
}
}
//下線を表示するかどうか
public bool IsUnderline
{
get
{
return (bool)GetValue(IsUnderlineProperty); ;
}
set
{
SetValue(IsUnderlineProperty, value);
}
}
//有効無効の設定(IsEnabledがoverrideできないため)
public bool IsDisabled
{
get
{
return !base.IsEnabled;
}
set
{
base.IsEnabled = !value;
OnEnabledChange(null, null);
}
}
//無効の場合に文字色を変更する
void OnEnabledChange(object sender, EventArgs e)
{
if (!base.IsEnabled)
{
this.TextColor = Color.Gray;
}
else
{
this.TextColor = Color.Blue;
}
}
//タップ時の動作
void OnClicked(object sender, EventArgs e)
{
//無効の場合はタップ時の動作をしない
if (!base.IsEnabled)
{
return;
}
//URL未設定の場合は処理を終了する
if (String.IsNullOrEmpty(this.Url))
{
return;
}
string err = String.Empty;
//外部ブラウザを起動しURLのサイトを表示する
DependencyService.Get<IWebBrowserService>().Open(new Uri(this.Url), err);
}
}
}
using UIKit;
using Foundation;
using System.ComponentModel;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
using AppName.Controls;
[assembly: ExportRenderer(typeof(BaseLinkButton), typeof(CustomLinkButtonRenderer))]
namespace AppName.iOS.Renderer
{
public class CustomLinkButtonRenderer : LabelRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
{
base.OnElementChanged(e);
if (Element == null)
{
return;
}
var view = (BaseLinkButton)Element;
UpdateUi(view, Control);
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (Element == null)
{
return;
}
var view = (BaseLinkButton)Element;
if (e.PropertyName == BaseLinkButton.IsUnderlineProperty.PropertyName)
{
UpdateUi(view, Control);
}
}
private static void UpdateUi(BaseLinkButton view, UILabel control)
{
var labelTitle = new NSMutableAttributedString(control.Text);
if (view.IsUnderline)
{
labelTitle.AddAttribute(UIStringAttributeKey.UnderlineStyle,
NSNumber.FromInt32((int)NSUnderlineStyle.Single),
new NSRange(0, labelTitle.Length));
}
control.AttributedText = labelTitle;
}
}
}
using Android.Widget;
using Android.Graphics;
using System.ComponentModel;
using Xamarin.Forms.Platform.Android;
using Xamarin.Forms;
using AppName.Controls;
[assembly: ExportRenderer(typeof(BaseLinkButton), typeof(AppName.Droid.Renderer.CustomLinkButtonRenderer))]
namespace AppName.Droid.Renderer
{
public class CustomLinkButtonRenderer : LabelRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
{
base.OnElementChanged(e);
var view = (BaseLinkButton)Element;
var control = Control;
UpdateUi(view, control);
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
var view = (BaseLinkButton)Element;
if (e.PropertyName == BaseLinkButton.IsUnderlineProperty.PropertyName)
{
Control.PaintFlags = view.IsUnderline ? Control.PaintFlags | PaintFlags.UnderlineText : Control.PaintFlags &= ~PaintFlags.UnderlineText;
}
}
static void UpdateUi(BaseLinkButton view, TextView control)
{
if (view.FontSize > 0)
{
control.TextSize = (float)view.FontSize;
}
if (view.IsUnderline)
{
control.PaintFlags = control.PaintFlags | PaintFlags.UnderlineText;
}
}
}
}
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:base="clr-namespace:AppName.Controls;assembly=AppName" >
<base:BaseLinkButton x:Name="linkButton1" Url="http://www.google.com/" Text="有効なリンク" IsDisabled="False" IsUnderline="True" HorizontalOptions="Center" />
<base:BaseLinkButton x:Name="linkButton2" Url="http://www.google.com/" Text="無効なリンク" IsDisabled="True" IsUnderline="True" HorizontalOptions="Center" />
<base:BaseLinkButton x:Name="linkButton3" Url="http://www.google.com/" Text="下線非表示リンク有効" IsDisabled="False" IsUnderline="False" HorizontalOptions="Center" />
<base:BaseLinkButton x:Name="linkButton4" Url="http://www.google.com/" Text="下線非表示リンク無効" IsDisabled="True" IsUnderline="False" HorizontalOptions="Center" />
<ContentPage>
※名前とタイトルが入力されていないコメントでは他のコメントとの区別ができません。
入力されていないコメントには返信しませんのであらかじめご了承くださいませ。