========================
CODE SNIPPETS
========================
TITLE: Platform-Specific Appium Setup
DESCRIPTION: Contains platform-specific details and configurations for Appium tests. Each class includes inline comments to describe the setup for Android, iOS, macOS, and Windows.
SOURCE: https://github.com/dotnet/maui-samples.git/blob/main/9.0/UITesting/BasicAppiumNunitSample/README.md#_snippet_1
LANGUAGE: csharp
CODE:
```
public class AppiumSetup
{
// Platform-specific Appium setup code
}
```
----------------------------------------
TITLE: Platform-Specific Appium Setup
DESCRIPTION: Contains platform-specific details and configurations for Appium tests. Each class includes inline comments to describe the setup for Android, iOS, macOS, and Windows.
SOURCE: https://github.com/dotnet/maui-samples.git/blob/main/10.0/UITesting/BasicAppiumNunitSample/README.md#_snippet_1
LANGUAGE: csharp
CODE:
```
public class AppiumSetup
{
// Platform-specific Appium setup code
}
```
----------------------------------------
TITLE: Map Control Setup
DESCRIPTION: This snippet shows the basic setup for using the Map control in .NET MAUI, including the necessary NuGet package and platform-specific configuration for Android.
SOURCE: https://github.com/dotnet/maui-samples.git/blob/main/9.0/UserInterface/Views/Map/README.md#_snippet_0
LANGUAGE: csharp
CODE:
```
// Add the Microsoft.Maui.Controls.Maps NuGet package
// For Android, obtain a Google Maps API key and insert it into the AndroidManifest.xml file.
// See: https://learn.microsoft.com/dotnet/maui/user-interface/controls/map#get-a-google-maps-api-key
```
LANGUAGE: xaml
CODE:
```
```
----------------------------------------
TITLE: .NET MAUI Shell Navigation Example
DESCRIPTION: Demonstrates navigation patterns and route management using .NET MAUI Shell. This sample highlights how to structure an application for intuitive user experience.
SOURCE: https://github.com/dotnet/maui-samples.git/blob/main/9.0/Apps/WhatToEat/README.md#_snippet_0
LANGUAGE: csharp
CODE:
```
// Example of Shell navigation setup (conceptual)
// In a real MAUI app, this would be in AppShell.xaml or related code-behind.
public partial class AppShell : Shell
{
public AppShell()
{
InitializeComponent();
// Define routes and navigation logic here
}
}
```
LANGUAGE: xaml
CODE:
```
```
----------------------------------------
TITLE: Map Control Setup
DESCRIPTION: This snippet shows the basic setup for using the Map control in .NET MAUI, including the necessary NuGet package and platform-specific configuration for Android.
SOURCE: https://github.com/dotnet/maui-samples.git/blob/main/10.0/UserInterface/Views/Map/README.md#_snippet_0
LANGUAGE: csharp
CODE:
```
// Add the Microsoft.Maui.Controls.Maps NuGet package
// For Android, obtain a Google Maps API key and insert it into the AndroidManifest.xml file.
// See: https://learn.microsoft.com/dotnet/maui/user-interface/controls/map#get-a-google-maps-api-key
```
LANGUAGE: xaml
CODE:
```
```
----------------------------------------
TITLE: .NET MAUI Shell Navigation Example
DESCRIPTION: Demonstrates navigation patterns and route management using .NET MAUI Shell. This sample highlights how to structure an application for intuitive user experience.
SOURCE: https://github.com/dotnet/maui-samples.git/blob/main/10.0/Apps/WhatToEat/README.md#_snippet_0
LANGUAGE: csharp
CODE:
```
// Example of Shell navigation setup (conceptual)
// In a real MAUI app, this would be in AppShell.xaml or related code-behind.
public partial class AppShell : Shell
{
public AppShell()
{
InitializeComponent();
// Define routes and navigation logic here
}
}
```
LANGUAGE: xaml
CODE:
```
```
----------------------------------------
TITLE: .NET MAUI FlyoutPage Navigation
DESCRIPTION: This C# code demonstrates the setup and navigation logic for a FlyoutPage in .NET MAUI. It typically involves setting up the main page and the flyout content, and handling navigation events.
SOURCE: https://github.com/dotnet/maui-samples.git/blob/main/9.0/Navigation/FlyoutPageSample/README.md#_snippet_0
LANGUAGE: csharp
CODE:
```
public partial class MainPage : FlyoutPage
{
public MainPage()
{
InitializeComponent();
}
}
```
----------------------------------------
TITLE: .NET MAUI FlyoutPage Navigation
DESCRIPTION: This C# code demonstrates the setup and navigation logic for a FlyoutPage in .NET MAUI. It typically involves setting up the main page and the flyout content, and handling navigation events.
SOURCE: https://github.com/dotnet/maui-samples.git/blob/main/10.0/Navigation/FlyoutPageSample/README.md#_snippet_0
LANGUAGE: csharp
CODE:
```
public partial class MainPage : FlyoutPage
{
public MainPage()
{
InitializeComponent();
}
}
```
----------------------------------------
TITLE: HybridWebView Communication Example
DESCRIPTION: This snippet illustrates how to set up and use the HybridWebView in .NET MAUI to facilitate communication between JavaScript in the web view and C# code in the application. It covers basic setup and message passing.
SOURCE: https://github.com/dotnet/maui-samples.git/blob/main/9.0/UserInterface/Views/HybridWebViewDemo/README.md#_snippet_0
LANGUAGE: csharp
CODE:
```
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
// Set the source for the HybridWebView
hybridWebView.Source = new HtmlWebViewSource
{
Html = "
Hello from MAUI!
",
BaseUrl = BaseUrl.CreateOptions()
};
// Register a JavaScript function to be callable from C#
hybridWebView.RegisterAction(async () => await DisplayAlert("Alert", "JavaScript function called!", "OK"));
}
// Method to be called from JavaScript
public void InvokeCSharpAction()
{
// This method will be called when the button in the HTML is clicked
// You can add your C# logic here
}
}
```
LANGUAGE: javascript
CODE:
```
function invokeCSharpAction() {
// This function is called from the HTML button click
// It invokes the registered C# action
window.chrome.webview.postMessage('Hello from JavaScript!');
}
```
LANGUAGE: xaml
CODE:
```
```
----------------------------------------
TITLE: HybridWebView Communication Example
DESCRIPTION: This snippet illustrates how to set up and use the HybridWebView in .NET MAUI to facilitate communication between JavaScript in the web view and C# code in the application. It covers basic setup and message passing.
SOURCE: https://github.com/dotnet/maui-samples.git/blob/main/10.0/UserInterface/Views/HybridWebViewDemo/README.md#_snippet_0
LANGUAGE: csharp
CODE:
```
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
// Set the source for the HybridWebView
hybridWebView.Source = new HtmlWebViewSource
{
Html = "
Hello from MAUI!
",
BaseUrl = BaseUrl.CreateOptions()
};
// Register a JavaScript function to be callable from C#
hybridWebView.RegisterAction(async () => await DisplayAlert("Alert", "JavaScript function called!", "OK"));
}
// Method to be called from JavaScript
public void InvokeCSharpAction()
{
// This method will be called when the button in the HTML is clicked
// You can add your C# logic here
}
}
```
LANGUAGE: javascript
CODE:
```
function invokeCSharpAction() {
// This function is called from the HTML button click
// It invokes the registered C# action
window.chrome.webview.postMessage('Hello from JavaScript!');
}
```
LANGUAGE: xaml
CODE:
```
```
----------------------------------------
TITLE: Install Appium and Drivers
DESCRIPTION: Installs Appium and the necessary drivers (XCUITest for iOS, UIAutomator2 for Android) required for building and running Appium tests.
SOURCE: https://github.com/dotnet/maui-samples.git/blob/main/9.0/UITesting/BrowserStackAppiumMaui/README.md#_snippet_4
LANGUAGE: bash
CODE:
```
npm install -g appium
# For Android
appium driver install uiautomator2
# For iOS
appium driver install xcuitest
```
----------------------------------------
TITLE: BrowserStack .NET SDK Setup for Apple Silicon Macs
DESCRIPTION: Installs and sets up the BrowserStack .NET SDK for macOS environments with Apple Silicon chips, ensuring compatibility for .NET toolchain.
SOURCE: https://github.com/dotnet/maui-samples.git/blob/main/9.0/UITesting/BrowserStackAppiumMaui/README.md#_snippet_5
LANGUAGE: bash
CODE:
```
dotnet tool install browserstack-sdk --version 1.16.3 --create-manifest-if-needed
dotnet browserstack-sdk setup-dotnet --dotnet-path "." --dotnet-version "8.0.403" --yes
```
----------------------------------------
TITLE: Install Appium and Drivers
DESCRIPTION: Installs Appium and the necessary drivers (XCUITest for iOS, UIAutomator2 for Android) required for building and running Appium tests.
SOURCE: https://github.com/dotnet/maui-samples.git/blob/main/10.0/UITesting/BrowserStackAppiumMaui/README.md#_snippet_4
LANGUAGE: bash
CODE:
```
npm install -g appium
# For Android
appium driver install uiautomator2
# For iOS
appium driver install xcuitest
```
----------------------------------------
TITLE: BrowserStack .NET SDK Setup for Apple Silicon Macs
DESCRIPTION: Installs and sets up the BrowserStack .NET SDK for macOS environments with Apple Silicon chips, ensuring compatibility for .NET toolchain.
SOURCE: https://github.com/dotnet/maui-samples.git/blob/main/10.0/UITesting/BrowserStackAppiumMaui/README.md#_snippet_5
LANGUAGE: bash
CODE:
```
dotnet tool install browserstack-sdk --version 1.16.3 --create-manifest-if-needed
dotnet browserstack-sdk setup-dotnet --dotnet-path "." --dotnet-version "8.0.403" --yes
```
----------------------------------------
TITLE: Label Hyperlink Example
DESCRIPTION: Shows how to create a hyperlink using a Label control with text decorations and a TapGestureRecognizer in .NET MAUI.
SOURCE: https://github.com/dotnet/maui-samples.git/blob/main/9.0/UserInterface/HyperlinkDemo/README.md#_snippet_0
LANGUAGE: csharp
CODE:
```
var label = new Label
{
Text = "Visit Microsoft",
TextDecorations = TextDecorations.Underline
};
var tapGesture = new TapGestureRecognizer();
tapGesture.Tapped += (s, e) => {
// Handle hyperlink tap
Launcher.OpenAsync(new Uri("https://www.microsoft.com"));
};
label.GestureRecognizers.Add(tapGesture);
```
LANGUAGE: xaml
CODE:
```
```
----------------------------------------
TITLE: .NET MAUI ListView Basic Usage
DESCRIPTION: This snippet demonstrates the fundamental setup of a .NET MAUI ListView, including its data binding and item template definition. It relies on the MAUI framework and requires a `DataTemplate` to define the appearance of each list item.
SOURCE: https://github.com/dotnet/maui-samples.git/blob/main/9.0/UserInterface/Views/ListViewDemos/README.md#_snippet_0
LANGUAGE: csharp
CODE:
```
public partial class ListViewPage : ContentPage
{
public ListViewPage()
{
InitializeComponent();
var items = new List { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
MyListView.ItemsSource = items;
}
}
```
LANGUAGE: xaml
CODE:
```
```
----------------------------------------
TITLE: Label Hyperlink Example
DESCRIPTION: Shows how to create a hyperlink using a Label control with text decorations and a TapGestureRecognizer in .NET MAUI.
SOURCE: https://github.com/dotnet/maui-samples.git/blob/main/10.0/UserInterface/HyperlinkDemo/README.md#_snippet_0
LANGUAGE: csharp
CODE:
```
var label = new Label
{
Text = "Visit Microsoft",
TextDecorations = TextDecorations.Underline
};
var tapGesture = new TapGestureRecognizer();
tapGesture.Tapped += (s, e) => {
// Handle hyperlink tap
Launcher.OpenAsync(new Uri("https://www.microsoft.com"));
};
label.GestureRecognizers.Add(tapGesture);
```
LANGUAGE: xaml
CODE:
```
```
----------------------------------------
TITLE: .NET MAUI ListView Basic Usage
DESCRIPTION: This snippet demonstrates the fundamental setup of a .NET MAUI ListView, including its data binding and item template definition. It relies on the MAUI framework and requires a `DataTemplate` to define the appearance of each list item.
SOURCE: https://github.com/dotnet/maui-samples.git/blob/main/10.0/UserInterface/Views/ListViewDemos/README.md#_snippet_0
LANGUAGE: csharp
CODE:
```
public partial class ListViewPage : ContentPage
{
public ListViewPage()
{
InitializeComponent();
var items = new List { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
MyListView.ItemsSource = items;
}
}
```
LANGUAGE: xaml
CODE:
```
```
----------------------------------------
TITLE: JavaScript Invoking .NET Sync Methods
DESCRIPTION: Demonstrates how to invoke synchronous .NET methods from JavaScript using `window.HybridWebView.InvokeDotNet`. Includes examples with and without parameters, and with return values.
SOURCE: https://github.com/dotnet/maui-samples.git/blob/main/9.0/UserInterface/Views/HybridWebViewDemo/HybridWebViewDemo/Resources/Raw/wwwroot/index.html#_snippet_3
LANGUAGE: javascript
CODE:
```
async function InvokeDoSyncWork() {
LogMessage("Invoking DoSyncWork");
await window.HybridWebView.InvokeDotNet('DoSyncWork');
LogMessage("Invoked DoSyncWork");
}
async function InvokeDoSyncWorkParams() {
LogMessage("Invoking DoSyncWorkParams");
await window.HybridWebView.InvokeDotNet('DoSyncWorkParams', [123, 'hello']);
LogMessage("Invoked DoSyncWorkParams");
}
async function InvokeDoSyncWorkReturn() {
LogMessage("Invoking DoSyncWorkReturn");
const retValue = await window.HybridWebView.InvokeDotNet('DoSyncWorkReturn');
LogMessage("Invoked DoSyncWorkReturn, return value: " + retValue);
}
async function InvokeDoSyncWorkParamsReturn() {
LogMessage("Invoking DoSyncWorkParamsReturn");
const retValue = await window.HybridWebView.InvokeDotNet('DoSyncWorkParamsReturn', [123, 'hello']);
LogMessage("Invoked DoSyncWorkParamsReturn, return value: message=" + retValue.Message + ", value=" + retValue.Value);
}
```
----------------------------------------
TITLE: JavaScript Invoking .NET Sync Methods
DESCRIPTION: Demonstrates how to invoke synchronous .NET methods from JavaScript using `window.HybridWebView.InvokeDotNet`. Includes examples with and without parameters, and with return values.
SOURCE: https://github.com/dotnet/maui-samples.git/blob/main/10.0/UserInterface/Views/HybridWebViewDemo/HybridWebViewDemo/Resources/Raw/wwwroot/index.html#_snippet_3
LANGUAGE: javascript
CODE:
```
async function InvokeDoSyncWork() {
LogMessage("Invoking DoSyncWork");
await window.HybridWebView.InvokeDotNet('DoSyncWork');
LogMessage("Invoked DoSyncWork");
}
async function InvokeDoSyncWorkParams() {
LogMessage("Invoking DoSyncWorkParams");
await window.HybridWebView.InvokeDotNet('DoSyncWorkParams', [123, 'hello']);
LogMessage("Invoked DoSyncWorkParams");
}
async function InvokeDoSyncWorkReturn() {
LogMessage("Invoking DoSyncWorkReturn");
const retValue = await window.HybridWebView.InvokeDotNet('DoSyncWorkReturn');
LogMessage("Invoked DoSyncWorkReturn, return value: " + retValue);
}
async function InvokeDoSyncWorkParamsReturn() {
LogMessage("Invoking DoSyncWorkParamsReturn");
const retValue = await window.HybridWebView.InvokeDotNet('DoSyncWorkParamsReturn', [123, 'hello']);
LogMessage("Invoked DoSyncWorkParamsReturn, return value: message=" + retValue.Message + ", value=" + retValue.Value);
}
```
----------------------------------------
TITLE: Consuming Package with UseSamplePackage()
DESCRIPTION: This C# snippet shows how to integrate the custom package into a .NET MAUI application. The UseSamplePackage() extension method, provided by the package, is called during the application's setup in MauiProgram.cs.
SOURCE: https://github.com/dotnet/maui-samples.git/blob/main/9.0/Packaging/NuGetWithMSBuildFiles/README.md#_snippet_5
LANGUAGE: cs
CODE:
```
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp()
.UseSamplePackage()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
});
return builder.Build();
}
}
```
----------------------------------------
TITLE: JavaScript Invoking .NET Async Methods
DESCRIPTION: Demonstrates how to invoke asynchronous .NET methods from JavaScript using `window.HybridWebView.InvokeDotNet`. Includes examples with and without parameters, and with return values.
SOURCE: https://github.com/dotnet/maui-samples.git/blob/main/9.0/UserInterface/Views/HybridWebViewDemo/HybridWebViewDemo/Resources/Raw/wwwroot/index.html#_snippet_4
LANGUAGE: javascript
CODE:
```
async function InvokeDoAsyncWork() {
LogMessage("Invoking DoAsyncWork");
await window.HybridWebView.InvokeDotNet('DoAsyncWork');
LogMessage("Invoked DoAsyncWork");
}
async function InvokeDoAsyncWorkParams() {
LogMessage("Invoking DoAsyncWorkParams");
await window.HybridWebView.InvokeDotNet('DoAsyncWorkParams', [123, 'hello']);
LogMessage("Invoked DoAsyncWorkParams");
}
async function InvokeDoAsyncWorkReturn() {
LogMessage("Invoking DoAsyncWorkReturn");
const retValue = await window.HybridWebView.InvokeDotNet('DoAsyncWorkReturn');
LogMessage("Invoked DoAsyncWorkReturn, return value: " + retValue);
}
async function InvokeDoAsyncWorkParamsReturn() {
LogMessage("Invoking DoAsyncWorkParamsReturn");
const retValue = await window.HybridWebView.InvokeDotNet('DoAsyncWorkParamsReturn', [123, 'hello']);
LogMessage("Invoked DoAsyncWorkParamsReturn, return value: message=" + retValue.Message + ", value=" + retValue.Value);
}
```
----------------------------------------
TITLE: Consuming Package with UseSamplePackage()
DESCRIPTION: This C# snippet shows how to integrate the custom package into a .NET MAUI application. The UseSamplePackage() extension method, provided by the package, is called during the application's setup in MauiProgram.cs.
SOURCE: https://github.com/dotnet/maui-samples.git/blob/main/10.0/Packaging/NuGetWithMSBuildFiles/README.md#_snippet_5
LANGUAGE: cs
CODE:
```
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp()
.UseSamplePackage()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
});
return builder.Build();
}
}
```
----------------------------------------
TITLE: JavaScript Invoking .NET Async Methods
DESCRIPTION: Demonstrates how to invoke asynchronous .NET methods from JavaScript using `window.HybridWebView.InvokeDotNet`. Includes examples with and without parameters, and with return values.
SOURCE: https://github.com/dotnet/maui-samples.git/blob/main/10.0/UserInterface/Views/HybridWebViewDemo/HybridWebViewDemo/Resources/Raw/wwwroot/index.html#_snippet_4
LANGUAGE: javascript
CODE:
```
async function InvokeDoAsyncWork() {
LogMessage("Invoking DoAsyncWork");
await window.HybridWebView.InvokeDotNet('DoAsyncWork');
LogMessage("Invoked DoAsyncWork");
}
async function InvokeDoAsyncWorkParams() {
LogMessage("Invoking DoAsyncWorkParams");
await window.HybridWebView.InvokeDotNet('DoAsyncWorkParams', [123, 'hello']);
LogMessage("Invoked DoAsyncWorkParams");
}
async function InvokeDoAsyncWorkReturn() {
LogMessage("Invoking DoAsyncWorkReturn");
const retValue = await window.HybridWebView.InvokeDotNet('DoAsyncWorkReturn');
LogMessage("Invoked DoAsyncWorkReturn, return value: " + retValue);
}
async function InvokeDoAsyncWorkParamsReturn() {
LogMessage("Invoking DoAsyncWorkParamsReturn");
const retValue = await window.HybridWebView.InvokeDotNet('DoAsyncWorkParamsReturn', [123, 'hello']);
LogMessage("Invoked DoAsyncWorkParamsReturn, return value: message=" + retValue.Message + ", value=" + retValue.Value);
}
```
----------------------------------------
TITLE: .NET MAUI Shell Mixed Navigation Example
DESCRIPTION: This code demonstrates the structure of a .NET MAUI application using Shell for mixed navigation, combining tabbed pages with a flyout menu. It sets up the basic navigation hierarchy for the application.
SOURCE: https://github.com/dotnet/maui-samples.git/blob/main/9.0/Navigation/ShellMixedSample/README.md#_snippet_0
LANGUAGE: csharp
CODE:
```
public partial class App : Application
{
public App()
{
InitializeComponent();
MainPage = new AppShell();
}
}
```
LANGUAGE: xaml
CODE:
```
```
----------------------------------------
TITLE: Theming with ResourceDictionaries and DynamicResource
DESCRIPTION: This snippet illustrates the core concept of theming in .NET MAUI. It involves defining separate ResourceDictionaries for each theme and using the DynamicResource markup extension to apply styles dynamically. This allows for runtime theme switching.
SOURCE: https://github.com/dotnet/maui-samples.git/blob/main/9.0/UserInterface/ThemingDemo/README.md#_snippet_0
LANGUAGE: csharp
CODE:
```
public partial class App : Application
{
public App()
{
InitializeComponent();
// Load the default theme
Application.Current.Resources.MergedDictionaries.Add(new LightTheme());
MainPage = new NavigationPage(new ThemingPage());
}
}
```
LANGUAGE: xaml
CODE:
```
```
LANGUAGE: xaml
CODE:
```
BlueWhite
```
LANGUAGE: xaml
CODE:
```
WhiteBlack
```
----------------------------------------
TITLE: .NET MAUI Shell Mixed Navigation Example
DESCRIPTION: This code demonstrates the structure of a .NET MAUI application using Shell for mixed navigation, combining tabbed pages with a flyout menu. It sets up the basic navigation hierarchy for the application.
SOURCE: https://github.com/dotnet/maui-samples.git/blob/main/10.0/Navigation/ShellMixedSample/README.md#_snippet_0
LANGUAGE: csharp
CODE:
```
public partial class App : Application
{
public App()
{
InitializeComponent();
MainPage = new AppShell();
}
}
```
LANGUAGE: xaml
CODE:
```
```
----------------------------------------
TITLE: Theming with ResourceDictionaries and DynamicResource
DESCRIPTION: This snippet illustrates the core concept of theming in .NET MAUI. It involves defining separate ResourceDictionaries for each theme and using the DynamicResource markup extension to apply styles dynamically. This allows for runtime theme switching.
SOURCE: https://github.com/dotnet/maui-samples.git/blob/main/10.0/UserInterface/ThemingDemo/README.md#_snippet_0
LANGUAGE: csharp
CODE:
```
public partial class App : Application
{
public App()
{
InitializeComponent();
// Load the default theme
Application.Current.Resources.MergedDictionaries.Add(new LightTheme());
MainPage = new NavigationPage(new ThemingPage());
}
}
```
LANGUAGE: xaml
CODE:
```
```
LANGUAGE: xaml
CODE:
```
BlueWhite
```
LANGUAGE: xaml
CODE:
```
WhiteBlack
```
----------------------------------------
TITLE: AppiumServerHelper.cs - Shared Test Code
DESCRIPTION: This C# code snippet is part of the UITests.Shared project and contains the AppiumServerHelper class. This class is responsible for starting and stopping the Appium server as part of the test execution process, simplifying the test setup.
SOURCE: https://github.com/dotnet/maui-samples.git/blob/main/9.0/UITesting/BrowserStackAppiumMaui/BasicAppiumNunitSample/README.md#_snippet_1
LANGUAGE: csharp
CODE:
```
using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using NUnit.Framework;
namespace UITests.Shared
{
public static class AppiumServerHelper
{
private static Process _appiumProcess;
public static void StartAppiumServer()
{
// Ensure Appium is installed and in PATH, or provide the full path to appium.cmd
string appiumPath = "appium"; // Assumes appium is in PATH
string arguments = "--base-path /wd/hub --port 4723"; // Example arguments
var startInfo = new ProcessStartInfo
{
FileName = appiumPath,
Arguments = arguments,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
};
try
{
_appiumProcess = Process.Start(startInfo);
// Give the server a moment to start up
Thread.Sleep(10000); // Adjust as needed
if (_appiumProcess.HasExited)
{
string errorOutput = _appiumProcess.StandardError.ReadToEnd();
throw new Exception($"Appium server failed to start. Error: {errorOutput}");
}
}
catch (Exception ex)
{
throw new Exception($"Failed to start Appium server: {ex.Message}");
}
}
public static void StopAppiumServer()
{
if (_appiumProcess != null && !_appiumProcess.HasExited)
{
try
{
_appiumProcess.Kill();
_appiumProcess.WaitForExit();
}
catch (Exception ex)
{
Console.WriteLine($"Error stopping Appium server: {ex.Message}");
}
}
}
}
}
```
----------------------------------------
TITLE: AppiumServerHelper.cs - Shared Test Code
DESCRIPTION: This C# code snippet is part of the UITests.Shared project and contains the AppiumServerHelper class. This class is responsible for starting and stopping the Appium server as part of the test execution process, simplifying the test setup.
SOURCE: https://github.com/dotnet/maui-samples.git/blob/main/10.0/UITesting/BrowserStackAppiumMaui/BasicAppiumNunitSample/README.md#_snippet_1
LANGUAGE: csharp
CODE:
```
using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using NUnit.Framework;
namespace UITests.Shared
{
public static class AppiumServerHelper
{
private static Process _appiumProcess;
public static void StartAppiumServer()
{
// Ensure Appium is installed and in PATH, or provide the full path to appium.cmd
string appiumPath = "appium"; // Assumes appium is in PATH
string arguments = "--base-path /wd/hub --port 4723"; // Example arguments
var startInfo = new ProcessStartInfo
{
FileName = appiumPath,
Arguments = arguments,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
};
try
{
_appiumProcess = Process.Start(startInfo);
// Give the server a moment to start up
Thread.Sleep(10000); // Adjust as needed
if (_appiumProcess.HasExited)
{
string errorOutput = _appiumProcess.StandardError.ReadToEnd();
throw new Exception($"Appium server failed to start. Error: {errorOutput}");
}
}
catch (Exception ex)
{
throw new Exception($"Failed to start Appium server: {ex.Message}");
}
}
public static void StopAppiumServer()
{
if (_appiumProcess != null && !_appiumProcess.HasExited)
{
try
{
_appiumProcess.Kill();
_appiumProcess.WaitForExit();
}
catch (Exception ex)
{
Console.WriteLine($"Error stopping Appium server: {ex.Message}");
}
}
}
}
}
```
----------------------------------------
TITLE: Local Notifications UI Setup
DESCRIPTION: This XAML snippet demonstrates how to set up the user interface for interacting with local notifications in a .NET MAUI application. It includes elements for triggering notifications and potentially displaying their status.
SOURCE: https://github.com/dotnet/maui-samples.git/blob/main/9.0/PlatformIntegration/LocalNotificationsDemo/README.md#_snippet_1
LANGUAGE: xaml
CODE:
```
```
----------------------------------------
TITLE: Build SamplePackage Project
DESCRIPTION: Command to build the SamplePackage project, which automatically generates the NuGet package.
SOURCE: https://github.com/dotnet/maui-samples.git/blob/main/9.0/Packaging/NuGetWithMSBuildFiles/README.md#_snippet_7
LANGUAGE: cli
CODE:
```
dotnet build src/SamplePackage/SamplePackage.csproj
```
----------------------------------------
TITLE: Build SamplePackage Project
DESCRIPTION: Command to build the SamplePackage project, which automatically generates the NuGet package.
SOURCE: https://github.com/dotnet/maui-samples.git/blob/main/10.0/Packaging/NuGetWithMSBuildFiles/README.md#_snippet_7
LANGUAGE: cli
CODE:
```
dotnet build src/SamplePackage/SamplePackage.csproj
```
----------------------------------------
TITLE: .NET MAUI CollectionView Basic Usage
DESCRIPTION: This snippet demonstrates the fundamental setup and usage of the CollectionView in .NET MAUI, typically involving XAML for layout and C# for data binding. It shows how to define a CollectionView and bind it to a data source.
SOURCE: https://github.com/dotnet/maui-samples.git/blob/main/9.0/UserInterface/Views/CollectionViewDemos/README.md#_snippet_0
LANGUAGE: csharp
CODE:
```
public partial class CollectionViewPage : ContentPage
{
public ObservableCollection Items { get; set; }
public CollectionViewPage()
{
InitializeComponent();
Items = new ObservableCollection {
"Item 1", "Item 2", "Item 3", "Item 4", "Item 5"
};
BindingContext = this;
}
}
```
LANGUAGE: xaml
CODE:
```
```
----------------------------------------
TITLE: .NET MAUI CollectionView Basic Usage
DESCRIPTION: This snippet demonstrates the fundamental setup and usage of the CollectionView in .NET MAUI, typically involving XAML for layout and C# for data binding. It shows how to define a CollectionView and bind it to a data source.
SOURCE: https://github.com/dotnet/maui-samples.git/blob/main/10.0/UserInterface/Views/CollectionViewDemos/README.md#_snippet_0
LANGUAGE: csharp
CODE:
```
public partial class CollectionViewPage : ContentPage
{
public ObservableCollection Items { get; set; }
public CollectionViewPage()
{
InitializeComponent();
Items = new ObservableCollection {
"Item 1", "Item 2", "Item 3", "Item 4", "Item 5"
};
BindingContext = this;
}
}
```
LANGUAGE: xaml
CODE:
```
```
----------------------------------------
TITLE: SamplePackage Project Configuration
DESCRIPTION: XML configuration for the SamplePackage .csproj file, including package metadata and output path.
SOURCE: https://github.com/dotnet/maui-samples.git/blob/main/9.0/Packaging/NuGetWithMSBuildFiles/README.md#_snippet_8
LANGUAGE: xml
CODE:
```
0.0.99999-sample../artifacts/true
```
----------------------------------------
TITLE: SamplePackage Project Configuration
DESCRIPTION: XML configuration for the SamplePackage .csproj file, including package metadata and output path.
SOURCE: https://github.com/dotnet/maui-samples.git/blob/main/10.0/Packaging/NuGetWithMSBuildFiles/README.md#_snippet_8
LANGUAGE: xml
CODE:
```
0.0.99999-sample../artifacts/true
```
----------------------------------------
TITLE: .NET MAUI Media Namespace Features
DESCRIPTION: Demonstrates features within the Microsoft.Maui.Media namespace, including media picking, taking screenshots, and text-to-speech conversion.
SOURCE: https://github.com/dotnet/maui-samples.git/blob/main/9.0/PlatformIntegration/PlatformIntegrationDemos/README.md#_snippet_4
LANGUAGE: csharp
CODE:
```
// Example for Media Picker (Image)
// var result = await MediaPicker.PickPhotoAsync();
// if (result != null) { var stream = await result.OpenReadAsync(); }
// Example for Screenshot
// var screenshot = await Screenshot.Default.CaptureAsync();
// var stream = await screenshot.OpenReadAsync();
// Example for Text-to-Speech
// await TextToSpeech.Default.SpeakAsync("Hello from MAUI");
// Example for Unit Converters (Conceptual - MAUI doesn't have a direct unit converter API, this is illustrative)
// double celsius = 25;
// double fahrenheit = (celsius * 9 / 5) + 32;
```
----------------------------------------
TITLE: RadialGradientBrush Example
DESCRIPTION: Shows how to use a RadialGradientBrush to paint an area with a gradient that radiates outwards from a central point.
SOURCE: https://github.com/dotnet/maui-samples.git/blob/main/9.0/UserInterface/BrushesDemos/README.md#_snippet_2
LANGUAGE: csharp
CODE:
```
var radialGradientBrush = new RadialGradientBrush
{
Center = new Point(0.5, 0.5),
Radius = 0.75,
GradientStops = new GradientStopCollection
{
new GradientStop(Colors.Yellow, 0.0f),
new GradientStop(Colors.Orange, 1.0f)
}
};
// Apply this brush to a control's background.
```
LANGUAGE: xaml
CODE:
```
```
----------------------------------------
TITLE: .NET MAUI Media Namespace Features
DESCRIPTION: Demonstrates features within the Microsoft.Maui.Media namespace, including media picking, taking screenshots, and text-to-speech conversion.
SOURCE: https://github.com/dotnet/maui-samples.git/blob/main/10.0/PlatformIntegration/PlatformIntegrationDemos/README.md#_snippet_4
LANGUAGE: csharp
CODE:
```
// Example for Media Picker (Image)
// var result = await MediaPicker.PickPhotoAsync();
// if (result != null) { var stream = await result.OpenReadAsync(); }
// Example for Screenshot
// var screenshot = await Screenshot.Default.CaptureAsync();
// var stream = await screenshot.OpenReadAsync();
// Example for Text-to-Speech
// await TextToSpeech.Default.SpeakAsync("Hello from MAUI");
// Example for Unit Converters (Conceptual - MAUI doesn't have a direct unit converter API, this is illustrative)
// double celsius = 25;
// double fahrenheit = (celsius * 9 / 5) + 32;
```
----------------------------------------
TITLE: RadialGradientBrush Example
DESCRIPTION: Shows how to use a RadialGradientBrush to paint an area with a gradient that radiates outwards from a central point.
SOURCE: https://github.com/dotnet/maui-samples.git/blob/main/10.0/UserInterface/BrushesDemos/README.md#_snippet_2
LANGUAGE: csharp
CODE:
```
var radialGradientBrush = new RadialGradientBrush
{
Center = new Point(0.5, 0.5),
Radius = 0.75,
GradientStops = new GradientStopCollection
{
new GradientStop(Colors.Yellow, 0.0f),
new GradientStop(Colors.Orange, 1.0f)
}
};
// Apply this brush to a control's background.
```
LANGUAGE: xaml
CODE:
```
```