3

I just created a small flyout:

MenuFlyout flyout = new MenuFlyout();
flyout.Items.Add(new X_UWP_App.Models.MyMenuFlyoutItem() { Text = "Copy" });
flyout.ShowAt(rect);

I can mark a text. After the marking of the text this flyout appears with "Copy" in it. Now I wondered how I could put some code behind "Copy"?

I was thinking of something like this, but it does not seem right.

public void onFlyoutItemClick(object sender, FlyoutItemClickEventArgs e)
{
    var dataPackage = new DataPackage();
    dataPackage.SetText(SelGetText());
    Clipboard.SetContent(dataPackage);
}

------ part above got answered. Under this line there is my next related question and answer ----

                var dataPackage = new DataPackage();
                dataPackage.SetText(m_view.vSelGetText());
                Clipboard.SetContent(dataPackage);

This is how those 3 lines really look. Note that m_view.vSelGetText() doesnt work. m_view is not assigned in this class. How could I achieve it so it is assigned. Because right now if I click on "Copy" it copies "Copy". This is the error I get: "An object reference is required for the non-static field, method, or property x.m_view"

axbeit
  • 853
  • 11
  • 35
  • What doesn't seem right about it? – mjwills Aug 28 '18 at 14:08
  • I couldnt find an Event for the "FlyoutItemClickEventArgs"-substitute. I am not even sure if it works like this. – axbeit Aug 28 '18 at 14:10
  • Quick suggestion. Try `Tapped` Event. – AVK Aug 28 '18 at 14:46
  • tried public void onFlyoutItemClick(object sender, FTappedEventHandler e) and public void onFlyoutItemClick(object sender, TappedRoutedEventArgs e). both didnt work. I might have misunderstood you, sorry if that is the case. – axbeit Aug 28 '18 at 14:52

1 Answers1

1

You code looks correct, you have implemented MyMenuFlyoutItem, you could add the onFlyoutItemClick in your class like the following.

class MyMenuFlyoutItem : MenuFlyoutItem
{
    public MyMenuFlyoutItem()
    {
        this.Click += MyMenuFlyoutItem_Click;
    }

    private void MyMenuFlyoutItem_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
    {
        var dataPackage = new DataPackage();
        dataPackage.SetText(SelGetText());
        Clipboard.SetContent(dataPackage);
    }

    private string SelGetText()
    {
        return this.Text;
    }
}
Nico Zhu
  • 32,367
  • 2
  • 15
  • 36
  • Hi. This worked out but I have a new problem now (my fault, I havent given you enough code). I will edit my main post for this. – axbeit Aug 29 '18 at 11:22
  • Doesn't this just set the clipboard to the word `Copy`...? – kayleeFrye_onDeck Jun 05 '19 at 22:56
  • 1
    If you change the constructor to accept a string as your clipboard content, you can just set that as a class property assigned to at construction and ditch the `SelGetText` method, and just swap that out with your new string property for the clipboard content. – kayleeFrye_onDeck Jun 05 '19 at 23:08