Skip to content
This repository was archived by the owner on May 15, 2024. It is now read-only.

Commit 920befd

Browse files
Add sample for hastext on clip & check IsNullOrEmpty (#1502)
1 parent afb692d commit 920befd

File tree

5 files changed

+12
-3
lines changed

5 files changed

+12
-3
lines changed

Samples/Samples/View/ClipboardPage.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<Entry Placeholder="Enter text..." Text="{Binding FieldValue}" />
1818
<Button Text="Copy to clipboard" Command="{Binding CopyCommand}" />
1919
<Button Text="Paste from clipboard" Command="{Binding PasteCommand}" />
20+
<Button Text="Check Status" Command="{Binding CheckCommand}" />
2021
<Label Text="{Binding LastCopied}" />
2122
</StackLayout>
2223
</ScrollView>

Samples/Samples/ViewModel/ClipboardViewModel.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,15 @@ public ClipboardViewModel()
1414
{
1515
CopyCommand = new Command(OnCopy);
1616
PasteCommand = new Command(OnPaste);
17+
CheckCommand = new Command(OnCheck);
1718
}
1819

1920
public ICommand CopyCommand { get; }
2021

2122
public ICommand PasteCommand { get; }
2223

24+
public ICommand CheckCommand { get; }
25+
2326
public string FieldValue
2427
{
2528
get => fieldValue;
@@ -72,5 +75,10 @@ async void OnPaste()
7275
FieldValue = text;
7376
}
7477
}
78+
79+
async void OnCheck()
80+
{
81+
await DisplayAlertAsync($"Has text: {Clipboard.HasText}");
82+
}
7583
}
7684
}

Xamarin.Essentials/Clipboard/Clipboard.android.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ static Task PlatformSetTextAsync(string text)
1717
}
1818

1919
static bool PlatformHasText
20-
=> Platform.ClipboardManager.HasPrimaryClip;
20+
=> Platform.ClipboardManager.HasPrimaryClip && !string.IsNullOrEmpty(Platform.ClipboardManager.PrimaryClip?.GetItemAt(0)?.Text);
2121

2222
static Task<string> PlatformGetTextAsync()
2323
=> Task.FromResult(Platform.ClipboardManager.PrimaryClip?.GetItemAt(0)?.Text);

Xamarin.Essentials/Clipboard/Clipboard.ios.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ static Task PlatformSetTextAsync(string text)
1616
static NSObject observer;
1717

1818
static bool PlatformHasText
19-
=> UIPasteboard.General.HasStrings;
19+
=> UIPasteboard.General.HasStrings && !string.IsNullOrEmpty(UIPasteboard.General.String);
2020

2121
static Task<string> PlatformGetTextAsync()
2222
=> Task.FromResult(UIPasteboard.General.String);

Xamarin.Essentials/Clipboard/Clipboard.macos.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ static Task PlatformSetTextAsync(string text)
2222
}
2323

2424
static bool PlatformHasText =>
25-
GetPasteboardText() != null;
25+
!string.IsNullOrEmpty(GetPasteboardText());
2626

2727
static Task<string> PlatformGetTextAsync()
2828
=> Task.FromResult(GetPasteboardText());

0 commit comments

Comments
 (0)