How to pin seconadry tile to open text file?

177 Views Asked by At

How I can pin secondary tile like Microsoft Edge websites, with tile ID, action, and activaton on App.xaml.cs? I want to pin opened text file on my text editor.

Update

This is my code for Pin tile:

        string tileId;

        string path = ((StorageFile)currentEditBox.Tag).Path;
        tileId = "file" + PivotMain.Items.Count;
        TileCollection colllaunch = new TileCollection();
        var mycoll = colllaunch.coll;
        mycoll.Add(tileId, path);
        // Use a display name you like
        string displayName;
        if(PivotMain.SelectedRichEditBoxItem.HeaderTextBlock.Text.Length > 10
            && currentEditBox.Tag != null)
        {
            displayName = ((StorageFile)currentEditBox.Tag).Name;
        }
        else
        {
            displayName = PivotMain.SelectedRichEditBoxItem.HeaderTextBlock.Text;
        }

        // Provide all the required info in arguments so that when user
        // clicks your tile, you can navigate them to the correct content
        string arguments = tileId;

        var imageUri = new Uri("ms-appx:///Assets/Square150x150Logo.scale-100.png");

        // During creation of secondary tile, an application may set additional arguments on the tile that will be passed in during activation.

        // Create a Secondary tile with all the required arguments.
        var secondaryTile = new SecondaryTile(tileId,
            displayName,
            arguments,
            imageUri, TileSize.Default);
        secondaryTile.VisualElements.ShowNameOnSquare150x150Logo = true;

        await secondaryTile.RequestCreateAsync();

This is code for tile collection:

    public class TileCollection
    {
        public Dictionary<string, string> coll = new Dictionary<string, string>();
    }

And this is for OnNavigatedTo event (I can't call open file event, because it is is on MainPage.xaml, and I moved it on MainPage):

        var launchArgs = e.Parameter as Windows.ApplicationModel.Activation.ILaunchActivatedEventArgs;
        if(launchArgs != null)
        {
            TileCollection colllaunch = new TileCollection();
            var mycoll = colllaunch.coll;
            if (launchArgs.TileId != "App")
            {
                StorageFile storageFile;
                string path;
                mycoll.TryGetValue(launchArgs.TileId, out path);
                storageFile = await StorageFile.GetFileFromPathAsync(path);
                await OpenFile(storageFile);
            }
        }
1

There are 1 best solutions below

18
On

When we set our secondary tile we will assign a tile ID like the following code:

 var secondaryTile = new SecondaryTile(“tile ID”,
  "App Name",
  "args",
  "tile",
  options,
  imageUri)
  { RoamingEnabled = true };

 await secondaryTile.RequestCreateAsync();

Then in the Onlaunched event we will be able to detect the tileID by the following code:

CollInit colllaunch = new CollInit();
var mycoll = colllaunch.coll;
if (e.PrelaunchActivated == false)
{
  if (rootFrame.Content == null)
  {
  // When the navigation stack isn't restored navigate to the first page,
  // configuring the new page by passing required information as a navigation
  // parameter
    if (e.TileId == "xxxxxx")
    {
      //Do what you want to do here
      var filepath=mycoll[xxxxxx];
      //Then navigate to a specific page
      rootFrame.Navigate(typeof(AlternatePage));

    }
    else
    {
      rootFrame.Navigate(typeof(MainPage), e.TileId);
    }
  }
  // Ensure the current window is active
  Window.Current.Activate();
}

There is a document on Technet wiki about how you can specify the content by ID.

------Update-----

For how to create a Dictionary and read it, here I just assume that you will not have a huge list, so a simple demo code:

public class CollInit
{
    public Dictionary<string, string> coll = new Dictionary<string, string>();
    public CollInit()
    {

        coll.Add("1233", "path1");
        coll.Add("1234", "path2");
        coll.Add("1235", "path3");
    }      
}

Then in onlaunched event you can read the path by create a new colloction like mycoll and then var mycoll = colllaunch.coll; after that you can read the path from mycoll[You specific TileID].