This repository was archived by the owner on May 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 493
Expand file tree
/
Copy pathBrowserViewModel.cs
More file actions
156 lines (133 loc) · 4.33 KB
/
BrowserViewModel.cs
File metadata and controls
156 lines (133 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows.Input;
using Xamarin.Essentials;
using Xamarin.Forms;
namespace Samples.ViewModel
{
public class BrowserViewModel : BaseViewModel
{
string browserStatus;
string uri = "http://xamarin.com";
int browserType = (int)BrowserLaunchMode.SystemPreferred;
int browserTitleType = (int)BrowserTitleMode.Default;
int controlColor = 0;
int toolbarColor = 0;
bool presentAsFormSheet = false;
bool presentAsPageSheet = false;
bool launchAdjacent = false;
Dictionary<string, Color> colorDictionary;
public List<string> AllColors { get; }
public BrowserViewModel()
{
OpenUriCommand = new Command(OpenUri);
colorDictionary = typeof(Color)
.GetFields()
.Where(f => f.FieldType == typeof(Color) && f.IsStatic && f.IsPublic)
.ToDictionary(f => f.Name, f => (Color)f.GetValue(null));
var colors = colorDictionary.Keys.ToList();
colors.Insert(0, "None");
AllColors = colors;
}
public ICommand OpenUriCommand { get; }
public string BrowserStatus
{
get => browserStatus;
set => SetProperty(ref browserStatus, value);
}
public string Uri
{
get => uri;
set => SetProperty(ref uri, value);
}
public List<string> BrowserLaunchModes { get; } =
new List<string>
{
$"Use System-Preferred Browser",
$"Use Default Browser App"
};
public int BrowserType
{
get => browserType;
set => SetProperty(ref browserType, value);
}
public List<string> BrowserTitleModes { get; } =
new List<string>
{
$"Use Default Mode",
$"Show Title",
$"Hide Title"
};
public int BrowserTitleType
{
get => browserTitleType;
set => SetProperty(ref browserTitleType, value);
}
public int ToolbarColor
{
get => toolbarColor;
set => SetProperty(ref toolbarColor, value);
}
public int ControlColor
{
get => controlColor;
set => SetProperty(ref controlColor, value);
}
public bool PresentAsFormSheet
{
get => presentAsFormSheet;
set => SetProperty(ref presentAsFormSheet, value);
}
public bool PresentAsPageSheet
{
get => presentAsPageSheet;
set => SetProperty(ref presentAsPageSheet, value);
}
public bool LaunchAdjacent
{
get => launchAdjacent;
set => SetProperty(ref launchAdjacent, value);
}
async void OpenUri()
{
if (IsBusy)
return;
IsBusy = true;
try
{
var flags = BrowserLaunchFlags.None;
if (PresentAsPageSheet)
flags |= BrowserLaunchFlags.PresentAsPageSheet;
if (PresentAsFormSheet)
flags |= BrowserLaunchFlags.PresentAsFormSheet;
if (LaunchAdjacent)
flags |= BrowserLaunchFlags.LaunchAdjacent;
await Browser.OpenAsync(uri, new BrowserLaunchOptions
{
LaunchMode = (BrowserLaunchMode)BrowserType,
TitleMode = (BrowserTitleMode)BrowserTitleType,
PreferredToolbarColor = GetColor(ToolbarColor),
PreferredControlColor = GetColor(ControlColor),
Flags = flags
});
}
catch (Exception e)
{
BrowserStatus = $"Unable to open Uri {e.Message}";
Debug.WriteLine(browserStatus);
}
finally
{
IsBusy = false;
}
Color? GetColor(int index)
{
return index <= 0
? (Color?)null
: (System.Drawing.Color)colorDictionary[AllColors[index]];
}
}
}
}