Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Important
This project is an experimental release. We hope you try out Experimental Mobile Blazor Bindings and provide feedback at https://github.com/xamarin/MobileBlazorBindings.
This article explains how to update an existing Mobile Blazor Bindings Preview 4 project to Mobile Blazor Bindings Preview 5. If your project is using an earlier preview, follow the previous migration guides and then proceed to this guide.
For more information see the announcement blog and release notes.
The following NuGet package references need to be updated in all of your solution's projects:
Microsoft.MobileBlazorBindings
version is0.5.50-preview
Xamarin.Forms
version is4.8.0.1451
or greaterXamarin.Essentials
version is1.5.3.2
or greater (if used in the project)Microsoft.Web.WebView2
- remove direct references to this package (from the Windows project) because it will be picked up transitively
To update the package versions:
- Open your application's solution in Visual Studio
- In Solution Explorer, right-click on the solution node and select "Manage NuGet Packages for Solution."
- Select the "Updates" tab
- Ensure that "Include prerelease" is checked
- Select all applicable packages to update, and ensure that the selected versions match the updated versions listed earlier
- Click "Update" and go through the proceeding dialogs. This operation can take a few minutes because it will download several NuGet packages and update all the relevant projects.
- It is recommended to Save All to ensure that all changes to CSPROJ files are committed to disk
You should now be able to build and run your project on this preview.
For non-hybrid apps the steps above cover the major changes regarding versions. The main other change is related to Grid layout syntax, which you can find below.
A Blazor Hybrid app created with Mobile Blazor Bindings Preview 4 requires several changes to update to Preview 5. The simplest way to migrate is to create a new project using Preview 5 and copying over all customizations.
To see all the changes in the default templates refer to Changes from Preview 4 v0.4.74-preview to Preview 5 v0.5.50-preview
If you want to make the changes individually, follow these steps:
In the shared UI project:
Move the
wwwroot
folder that contains static web assets to the root of the project (it used to be a sub-folder of theWebUI
folder)In
Main.razor
remove theContentRoot
property from the<BlazorWebView>
controlIn
App.cs
change the constructor to match this code (while preserving any customizations you have made, such as registering app-specific services):public App(IFileProvider fileProvider = null) { var hostBuilder = MobileBlazorBindingsHost.CreateDefaultBuilder() .ConfigureServices((hostContext, services) => { // Adds web-specific services such as NavigationManager services.AddBlazorHybrid(); // Register app-specific services services.AddSingleton<CounterState>(); }) .UseWebRoot("wwwroot"); if (fileProvider != null) { hostBuilder.UseStaticFiles(fileProvider); } else { hostBuilder.UseStaticFiles(); } var host = hostBuilder.Build(); MainPage = new ContentPage { Title = "My Application" }; host.AddComponent<Main>(parent: MainPage); }
In the CSPROJ file remove the
<WwwRootResourcePath>
element and remove the<EmbeddedResource ... />
element that references it. (The project should no longer mentionWwwRootResourcePath
at all).If the project has code that reads a static file in the project (such as the default template's
WebUI/Pages/FetchData.razor
file), do the following instead:Inject the file provider service by adding
@inject Microsoft.Extensions.FileProviders.IFileProvider FileProvider
to the.razor
fileTry to open the file and read it by calling:
var fileInfo = FileProvider.GetFileInfo("_content/PROJECT_NAME/filename.json"); if (fileInfo != null && fileInfo.Exists) { using (var stream = fileInfo.CreateReadStream()) { var data = await JsonSerializer.DeserializeAsync<DATA_TYPE>(stream, ...); } }
For Android:
- In
MainActivity.cs
add a constructor argument to theApp
constructor:LoadApplication(new App(new AssetFileProvider(Assets, "wwwroot")));
. Import any required namespaces, such asMicrosoft.MobileBlazorBindings.WebView.Android
. - Create a folder in the Android project called
wwwroot
folder and copy thewwwroot/index.html
file from the shared UI project there. Then right-click on the file and select Properties. Set its Build Action toNone
and setCopy if newer
.
- In
For iOS:
- Create a folder in the iOS project called
Resources/wwwroot
folder and copy thewwwroot/index.html
file from the shared UI project there. Then right-click on the file and select Properties. Ensure that the Build Action is set toContent
.
- Create a folder in the iOS project called
For macOS:
- Create a folder in the macOS project called
Resources/wwwroot
folder and copy thewwwroot/index.html
file from the shared UI project there. Then right-click on the file and select Properties. Ensure that the Build Action is set toContent
.
- Create a folder in the macOS project called
For Windows:
- Create a folder in the Windows project called
wwwroot
folder and copy thewwwroot/index.html
file from the shared UI project there. Then right-click on the file and select Properties. Ensure that the Build Action is set toContent
and setCopy if newer
.
- Create a folder in the Windows project called
After completing the previous steps, you can go back to the shared UI project and delete
wwwroot/index.html
because it is now in the platform-specific projects
The Grid control's syntax for row and column definitions has been simplified:
- Remove the
<Layout>
and<Contents>
sections from the<Grid>
control - Have the grid's row and column definitions defined as comma-separated strings instead of as individual collection items
In Preview 4 and earlier the Grid had <Layout>
and <Contents>
sections, and multiple <RowDefinition>
/<ColumnDefinition>
items:
<Grid VerticalOptions="LayoutOptions.FillAndExpand">
<Layout>
<RowDefinition GridUnitType="GridUnitType.Auto" />
<RowDefinition GridUnitType="GridUnitType.Auto" />
<RowDefinition GridUnitType="GridUnitType.Auto" />
<ColumnDefinition GridUnitType="GridUnitType.Auto" />
<ColumnDefinition GridUnitType="GridUnitType.Auto" />
</Layout>
<Contents>
<GridCell Row="0" Column="0" ColumnSpan="2">
<Label BackgroundColor="Color.LightBlue" Text="Todo items" FontSize="20" />
</GridCell>
<GridCell Row="1" Column="0">
<Switch IsToggled="true" />
</GridCell>
<GridCell Row="1" Column="1">
<Label Text="Use awesome Xamarin.Forms features" FontSize="20" />
</GridCell>
<GridCell Row="2" Column="0">
<Switch IsToggled="true" />
</GridCell>
<GridCell Row="2" Column="1">
<Label Text="Delight our customers" FontSize="20" />
</GridCell>
</Contents>
</Grid>
Starting in Preview5 the <Layout>
and <Content>
sections are removed, and all row/column definitions are specified as strings (9 lines of markup REMOVED!):
<Grid VerticalOptions="LayoutOptions.FillAndExpand" RowDefinitions="Auto, Auto, Auto" ColumnDefinitions="Auto, Auto">
<GridCell Row="0" Column="0" ColumnSpan="2">
<Label BackgroundColor="Color.LightBlue" Text="Todo items" FontSize="20" />
</GridCell>
<GridCell Row="1" Column="0">
<Switch IsToggled="true" />
</GridCell>
<GridCell Row="1" Column="1">
<Label Text="Use awesome Xamarin.Forms features" FontSize="20" />
</GridCell>
<GridCell Row="2" Column="0">
<Switch IsToggled="true" />
</GridCell>
<GridCell Row="2" Column="1">
<Label Text="Delight our customers" FontSize="20" />
</GridCell>
</Grid>
For more Grid information, refer to the Grid Layout topic.