Porting Blazor WASM to Blazor MAUI #8041
Replies: 4 comments 1 reply
-
|
Beta Was this translation helpful? Give feedback.
-
You can't use
Whether or not you need |
Beta Was this translation helpful? Give feedback.
-
Most of the JS interop is used in a UI components library that is specifically tailored to the needs of the type of applications that I'm designing. I've looked at commercial alternatives but they are all bloated and often don't support some basic features that I need (although offer plenty of other features that I don't care about). So I created my own component set (at this point it's a library of about 30 components). Each component has its own JS module that handles any DOM interaction that cannot be done via Blazor. I'm invoking module functions asynchronously (no problem here) - it's the only supported option anyway.
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
m_componentRef = DotNetObjectReference.Create((BaseJsInteropComponent)this);
m_module = await ImportModuleAsync("BlzComboBox.js");
if (m_isDisposed == false)
{
await m_module.InvokeVoidAsync("register", m_componentRef, m_componentGuid, m_inptDropdown, m_btnDropdownToggle, m_divDropdownMenu, m_btnClear);
}
m_isAfterFirstRender = true;
}
if (m_afterRenderActionTypeId == AfterRenderActionType.ScrollToSelectedItem)
{
// scroll selected <a> element into view, positioning the item at the top of the container
// note that certain component vendors scroll selected item to the bottom of the container instead (which requires extra logic)
await m_module.InvokeVoidAsync("scrollToSelectedItem", m_componentGuid);
m_afterRenderActionTypeId = AfterRenderActionType.None;
}
else if (m_afterRenderActionTypeId == AfterRenderActionType.Open)
{
// open dropdown below input element
await m_module.InvokeVoidAsync("open", m_componentGuid);
m_afterRenderActionTypeId = AfterRenderActionType.None;
}
}
export function open(componentGuid) {
const component = lib.getComponent(componentGuid);
lib.registerOpenDropdownMenuComponent(componentGuid);
lib.updateDropdownMenuPosition(component.inptDropdown, component.divDropdownMenu);
scrollToSelectedItem(componentGuid);
component.inptDropdown.focus();
}
private void Hide()
{
IsDropdownMenuVisible = false;
JSRuntime.InvokeVoid("lib.unregisterOpenDropdownMenuComponent", m_componentGuid);
} public class LibState : IDisposable
{
public event Action ThemeChanged;
public DeviceType DeviceTypeId { get; set; }
private readonly DotNetObjectReference<LibState> m_componentRef;
private readonly IJSInProcessRuntime m_jSRuntime;
public LibState(IJSInProcessRuntime jSRuntime)
{
m_jSRuntime = jSRuntime;
m_componentRef = DotNetObjectReference.Create(this);
DeviceTypeId = (DeviceType)Enum.Parse(typeof(DeviceType), jSRuntime.Invoke<string>("lib.getDeviceType"));
}
public void ChangeTheme(string cssFileUrl)
=> m_jSRuntime.InvokeVoid("lib.changeAppTheme", cssFileUrl, m_componentRef);
[JSInvokable]
public void OnThemeChanged()
=> ThemeChanged?.Invoke();
public void Dispose()
=> m_componentRef?.Dispose(); As you can see, I'm calling In case it's not clear,
window.lib = {
deviceType: null,
components: [],
registerComponent: (componentDotNetRef, componentGuid, type) => {
...
} What do you recommend? Issuing synchronous JS calls was a carefully scrutinized decision, when I started the work on my components there was no .NET MAUI and I knew I had no interest in Blazor server. Now that .NET MAUI is here, I absolutely love the idea of being able to use the same code and publish an Android or iOS app!!! You only mentioned I don't even know how to start experimenting with this, I couldn't find any useful documentation. I'm OK to wait for the first official preview which should be coming up soon I believe. I'm looking forward to your comments @SteveSandersonMS ! |
Beta Was this translation helpful? Give feedback.
-
It seems all the previous issues have been locked for conversation, makes it hard to jump in at any given point in the repo. I started looking at Maui Blazor Hybrid, but have yet, after days of investigation, figure out how to properly interop with JavaScript to load external libraries from the JS library to integrate with my app. All the documentation seems to pertain specifically to ASP or Blazor WASM and is not compatible with Maui Blazor Hybrid, but that's not called out anywhere. There's no documentation for the I've loaded a library which I know loads a JavaScript based component and able to run it within my app, so there is some way to do this. But I don't understand how that library has done that and haven't found any valid documentation to show a simple case of how to perform this interop within a simple Blazor component within a Maui Blazor Hybrid app. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
I have created a Blazor WASM app and now I'd like to run it as an Android (and later iOS) app. My understanding is that this is possible using this new technology.
IJSInProcessRuntime
for all JS interop and callStateHasChanged
rather thanawait InvokeAsync(StateHasChanged)
- I'm hoping this is not going to be a problem?Thank you!
Beta Was this translation helpful? Give feedback.
All reactions