Share Mvvm for inkCanvas

Javier R 211 Reputation points
2021-07-06T15:30:58.617+00:00

Hello:

I have an app mvvm of inkcanvas as I can implement share in the file.Cs

Universal Windows Platform (UWP)
{count} votes

2 answers

Sort by: Most helpful
  1. Roy Li - MSFT 32,731 Reputation points Microsoft Vendor
    2021-07-08T03:07:00.5+00:00

    Hello,

    Welcome to Microsoft Q&A!

    If you want to put the code into a ViewModel, the first step is that you need to create a custom Command class that implements the ICommand interface. Then you could bind the click event to the Command in your ViewModel. When the click command is fired, call other methods in the ViewModel to finish your logic.

    I've made a simple demo about this:
    Xaml code

     <Grid>  
            <Button Content="{x:Bind testmodel.Name}" Command="{x:Bind testmodel.ClickCommand}"/>  
        </Grid>  
    
     
    

    Code behind

        public sealed partial class MainPage : Page  
        {  
            public ViewModel testmodel { get; set; }  
            public MainPage()  
            {  
                this.InitializeComponent();  
                testmodel = new ViewModel();  
            }  
      
        }  
    

    ViewModel Class

       public class ViewModel   
        {  
            public string Name { get; set; }  
            //the click command  
            public RelayCommand ClickCommand { get; private set; }  
      
            public ViewModel()  
            {  
                Name = "TestName";  
                ClickCommand = new RelayCommand(s => { ShareData(); }, true);  
            }  
      
            public async void ShareData()   
            {  
                StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync("MyPen.png",  
                     Windows.Storage.CreationCollisionOption.ReplaceExisting);  
                DataTransferManager dataTransferManager = DataTransferManager.GetForCurrentView();  
                dataTransferManager.DataRequested += DataTransferManager_DataRequested;  
      
                DataTransferManager.ShowShareUI();  
            }  
      
            private async void DataTransferManager_DataRequested(DataTransferManager sender, DataRequestedEventArgs args)  
            {  
                 //....some logic code  
            }  
        }  
    

    Custom Command

      //custom command class  
        public class RelayCommand : ICommand  
        {  
            private Action<object> action;  
            private bool canExecute;  
            public event EventHandler CanExecuteChanged;  
      
            public RelayCommand(Action<object> action, bool canExecute)  
            {  
                this.action = action;  
                this.canExecute = canExecute;  
            }  
      
            public bool CanExecute(object parameter)  
            {  
                return canExecute;  
            }  
      
            public void Execute(object parameter)  
            {  
                action(parameter);  
            }  
        }  
    

    Thank you.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Javier R 211 Reputation points
    2021-07-08T17:56:31.813+00:00
    public  class ShareTarget
        {
            public ShareTarget()
            {
                DataTransferManager.GetForCurrentView().DataRequested += OnShareDataRequested;
            }
    
           ~ShareTarget()
            {
                DataTransferManager.GetForCurrentView().DataRequested -= OnShareDataRequested;
            }
    
    
    
            private InkCanvas inkCanvas;
    
            private RandomAccessStreamReference strokeStream;
    
            public void OnShare() => DataTransferManager.ShowShareUI();
    
    
    
            private async void OnShareDataRequested(DataTransferManager sender, DataRequestedEventArgs args)
            {
    
                var deferral = args.Request.GetDeferral();
    
                await ShareData();
    
                deferral.Complete();
    
            }
    
    
            private async Task ShareData(DataRequest datarequest)
            {
                datarequest.Data.Properties.Title = "InkPen";
                datarequest.Data.Properties.Description = "send InkPen";
    
                var file = await ApplicationData.Current.LocalFolder.CreateFileAsync("MyPen.png",
                    Windows.Storage.CreationCollisionOption.ReplaceExisting);
                using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite))
                {
                    await inkCanvas.InkPresenter.StrokeContainer.SaveAsync(stream);
                    strokeStream = RandomAccessStreamReference.CreateFromFile(file);
                }
    
    
                 datarequest.Data.SetBitmap(strokeStream);
    
    
    
            }
    
    
        }
    }
    

    I'm doing Sahre in a calse to then use in disinta app page I havea doubt ShareData in the final part and OnShareDataRequested