Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RxUI application as a target for "sharing content" #1001

Open
ghuntley opened this issue Dec 21, 2015 · 1 comment
Open

RxUI application as a target for "sharing content" #1001

ghuntley opened this issue Dec 21, 2015 · 1 comment

Comments

@ghuntley
Copy link
Member

ghuntley [10:31 PM] @flagbug: I'm doing UWP currently, as is @moswald so
between us we got that covered.

flagbug [10:34 PM] @ghuntley: Beware, there are some major issues with RxUI
if your app is a target for a share operation: 

https://msdn.microsoft.com/en-us/library/windows/apps/hh465251.aspx
because of the way that RxApp.MainThreadScheduler and friends work.

ghuntley [10:39 PM] @flagbug: can you give some examples of what you have
observed? Thanks for the heads up, not using sharing in this app fortunately.

flagbug [10:41 PM] @ghuntley: You have to set `RxApp.MainThreadScheduler`
when opening the app as share target, since the share target UI gets its own UI
thread and then, when you close the share UI, you have to set
`RxApp.MainThreadScheduler` back to its previous value, or else your main
application will crash.

See also #965

@flagbug
Copy link
Member

flagbug commented Dec 21, 2015

As a workaround, I'm doing something like this in the OnShareTargetActivated method:

// Capture the UI thread scheduler of the main application, so we can reset it, when we
// exit the share flyout
IScheduler realMainThreadScheduler = RxApp.MainThreadScheduler;
RxApp.MainThreadScheduler = new WaitForDispatcherScheduler(() => CoreDispatcherScheduler.Current);

// Pretty much the same goes for the DefaulltItemTemplate, since the template is
// generated on a different UI thread, we have to reset it here and change it back when
// closing the share target
// 
// See https://github.com/reactiveui/ReactiveUI/issues/965 for more information
var previousTemplate = AutoDataTemplateBindingHook.DefaultItemTemplate;
AutoDataTemplateBindingHook.DefaultItemTemplate = new Lazy<DataTemplate>(() =>
{
    const string template = "<DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:xaml='using:ReactiveUI'>" +
        "<xaml:ViewModelViewHost ViewModel=\"{Binding}\" VerticalContentAlignment=\"Stretch\" HorizontalContentAlignment=\"Stretch\" IsTabStop=\"False\" />" +
    "</DataTemplate>";
    var assemblyName = "";

    return (DataTemplate)XamlReader.Load(template.Replace("__ASSEMBLYNAME__", assemblyName));
});

Window.Current.Activate();
Window.Current.VisibilityChanged += (sender, ev) =>
{
    if (!ev.Visible)
    {
        RxApp.MainThreadScheduler = realMainThreadScheduler;
        AutoDataTemplateBindingHook.DefaultItemTemplate = previousTemplate;
    }
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants