{"id":37511,"date":"2024-11-01T09:58:08","date_gmt":"2024-11-01T09:58:08","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=37511"},"modified":"2024-11-01T11:02:26","modified_gmt":"2024-11-01T11:02:26","slug":"uwp-development-routed-event","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/37511\/","title":{"rendered":"UWP Development, Routed Event"},"content":{"rendered":"<p>In Universal Windows Platform (UWP) development, events play an important role. Events are essential elements that connect the user interface (UI) and user interactions. To handle events, event handlers are needed, and Routed Events describe the mechanism by which events propagate within a UWP application. In this article, we will explore the concept of Routed Events, usage examples, and various programming techniques in detail.<\/p>\n<h2>What is a Routed Event?<\/h2>\n<p>A Routed Event is a pattern for events that occur in UWP, where events can propagate from the element that raised them to its parent element or from a parent element to its child elements. This structure allows events to be handled only at desired locations, simplifying interactions between various elements in the UI tree.<\/p>\n<p>Routed Events have three main propagation directions:<\/p>\n<ul>\n<li><strong>Bubbling<\/strong>: Events propagate from child elements to parent elements. This usually occurs when UI elements are manipulated.<\/li>\n<li><strong>Tunneling<\/strong>: Events travel down from parent elements to child elements. This method is useful when you want to use higher-priority event handlers.<\/li>\n<li><strong>Direct<\/strong>: Events are handled directly by specific elements. This is used when performing basic event handling.<\/li>\n<\/ul>\n<h2>Creating a Routed Event<\/h2>\n<p>Routed Events can be created as custom events. Here\u2019s how to create a Routed Event in UWP:<\/p>\n<pre><code>csharp\n\/\/ Registering an event with RoutedEventManager\npublic static readonly RoutedEvent MyCustomEvent = EventManager.RegisterRoutedEvent(\n    \"MyCustom\",\n    RoutingStrategy.Bubbling,\n    typeof(RoutedEventHandler),\n    typeof(MyControl));\n\npublic event RoutedEventHandler MyCustom\n{\n    add { AddHandler(MyCustomEvent, value); }\n    remove { RemoveHandler(MyCustomEvent, value); }\n}\n\n\/\/ Method to raise the event\nprotected virtual void RaiseMyCustomEvent()\n{\n    RoutedEventArgs args = new RoutedEventArgs(MyCustomEvent);\n    RaiseEvent(args);\n}\n<\/code><\/pre>\n<p>In the code above, we created a custom control called <strong>MyControl<\/strong> and registered a Routed Event named <strong>MyCustomEvent<\/strong>. Event handlers can be added and removed via the <strong>MyCustom<\/strong> property. Finally, the <strong>RaiseMyCustomEvent<\/strong> method is responsible for raising the event.<\/p>\n<h2>Using Routed Events<\/h2>\n<p>Now that we have created a custom Routed Event, it&#8217;s time to actually use it. Let&#8217;s see how we can leverage Routed Events through the example below:<\/p>\n<pre><code>csharp\n\/\/ Custom control class\npublic sealed class MyControl : Control\n{\n    public MyControl()\n    {\n        this.DefaultStyleKey = typeof(MyControl);\n        this.PointerPressed += OnPointerPressed; \/\/ Handling pointer click events\n    }\n\n    private void OnPointerPressed(object sender, PointerRoutedEventArgs e)\n    {\n        RaiseMyCustomEvent(); \/\/ Raising MyCustom event\n    }\n}\n\n\/\/ Using the custom control in XAML\n&lt;Page\n    x:Class=\"MyApp.MainPage\"\n    xmlns=\"http:\/\/schemas.microsoft.com\/winfx\/2006\/xaml\/presentation\"\n    xmlns:x=\"http:\/\/schemas.microsoft.com\/winfx\/2006\/xaml\"\n    xmlns:local=\"using:MyApp\"\n    xmlns:d=\"http:\/\/schemas.microsoft.com\/expression\/blend\/2008\"\n    xmlns:mc=\"http:\/\/schemas.openxmlformats.org\/markup-compatibility\/2006\"\n    mc:Ignorable=\"d\"&gt;\n\n    &lt;StackPanel&gt;\n        &lt;local:MyControl MyCustom=\"MyControl_MyCustom\"\/&gt;\n    &lt;\/StackPanel&gt;\n&lt;\/Page&gt;\n\n\/\/ Event handler (XAML.cs)\nprivate void MyControl_MyCustom(object sender, RoutedEventArgs e)\n{\n    \/\/ Handling the custom event\n    Debug.WriteLine(\"MyCustom Routed Event occurred!\");\n}\n<\/code><\/pre>\n<p>In the code above, we create <strong>MyControl<\/strong> and raise the <strong>MyCustom<\/strong> event when the user clicks. It demonstrates how to connect this event in XAML for handling. When the event occurs, the <strong>MyControl_MyCustom<\/strong> method is called to print a message in the debug log.<\/p>\n<h2>Routed Events and Delegates<\/h2>\n<p>In UWP, Routed Events are implemented through <strong>Delegates<\/strong>. Routed Event handlers have specific signatures that are associated with the routed events. Here\u2019s an example using Delegate:<\/p>\n<pre><code>csharp\npublic delegate void CustomEventHandler(object sender, CustomEventArgs e);\npublic event CustomEventHandler CustomEvent;\n\nprotected virtual void OnCustomEvent(CustomEventArgs e)\n{\n    CustomEvent?.Invoke(this, e); \/\/ Raise the event\n}\n<\/code><\/pre>\n<p>Using Delegates like this allows for implementing complex logic and sending custom events with additional event data.<\/p>\n<h2>Conclusion<\/h2>\n<p>Routed Events are a powerful tool that enables efficient handling of user interactions with the user interface in UWP development. We examined how to raise and handle events, create custom events, and utilize Delegates. With this understanding, you can enhance the interface and user experience of your UWP applications.<\/p>\n<p>Now, we hope you will leverage Routed Events to develop better UWP apps. If you have any questions, feel free to ask in the comments!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Universal Windows Platform (UWP) development, events play an important role. Events are essential elements that connect the user interface (UI) and user interactions. To handle events, event handlers are needed, and Routed Events describe the mechanism by which events propagate within a UWP application. In this article, we will explore the concept of Routed &hellip; <a href=\"https:\/\/atmokpo.com\/w\/37511\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;UWP Development, Routed Event&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[115],"tags":[],"class_list":["post-37511","post","type-post","status-publish","format-standard","hentry","category-uwp-programming"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>UWP Development, Routed Event - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/atmokpo.com\/w\/37511\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"UWP Development, Routed Event - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"In Universal Windows Platform (UWP) development, events play an important role. Events are essential elements that connect the user interface (UI) and user interactions. To handle events, event handlers are needed, and Routed Events describe the mechanism by which events propagate within a UWP application. In this article, we will explore the concept of Routed &hellip; \ub354 \ubcf4\uae30 &quot;UWP Development, Routed Event&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/37511\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:58:08+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:02:26+00:00\" \/>\n<meta name=\"author\" content=\"root\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:site\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:label1\" content=\"\uae00\uc4f4\uc774\" \/>\n\t<meta name=\"twitter:data1\" content=\"root\" \/>\n\t<meta name=\"twitter:label2\" content=\"\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04\" \/>\n\t<meta name=\"twitter:data2\" content=\"3\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/37511\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/37511\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"UWP Development, Routed Event\",\"datePublished\":\"2024-11-01T09:58:08+00:00\",\"dateModified\":\"2024-11-01T11:02:26+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/37511\/\"},\"wordCount\":451,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"UWP Programming\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/37511\/\",\"url\":\"https:\/\/atmokpo.com\/w\/37511\/\",\"name\":\"UWP Development, Routed Event - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:58:08+00:00\",\"dateModified\":\"2024-11-01T11:02:26+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/37511\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/37511\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/37511\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"UWP Development, Routed Event\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/atmokpo.com\/w\/#website\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/atmokpo.com\/w\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ko-KR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"contentUrl\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"width\":400,\"height\":400,\"caption\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\"},\"image\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/bebubo4\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\",\"name\":\"root\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"caption\":\"root\"},\"sameAs\":[\"http:\/\/atmokpo.com\/w\"],\"url\":\"https:\/\/atmokpo.com\/w\/author\/root\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"UWP Development, Routed Event - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/atmokpo.com\/w\/37511\/","og_locale":"ko_KR","og_type":"article","og_title":"UWP Development, Routed Event - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"In Universal Windows Platform (UWP) development, events play an important role. Events are essential elements that connect the user interface (UI) and user interactions. To handle events, event handlers are needed, and Routed Events describe the mechanism by which events propagate within a UWP application. In this article, we will explore the concept of Routed &hellip; \ub354 \ubcf4\uae30 \"UWP Development, Routed Event\"","og_url":"https:\/\/atmokpo.com\/w\/37511\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:58:08+00:00","article_modified_time":"2024-11-01T11:02:26+00:00","author":"root","twitter_card":"summary_large_image","twitter_creator":"@bebubo4","twitter_site":"@bebubo4","twitter_misc":{"\uae00\uc4f4\uc774":"root","\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04":"3\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/37511\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/37511\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"UWP Development, Routed Event","datePublished":"2024-11-01T09:58:08+00:00","dateModified":"2024-11-01T11:02:26+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/37511\/"},"wordCount":451,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["UWP Programming"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/37511\/","url":"https:\/\/atmokpo.com\/w\/37511\/","name":"UWP Development, Routed Event - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:58:08+00:00","dateModified":"2024-11-01T11:02:26+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/37511\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/37511\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/37511\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"UWP Development, Routed Event"}]},{"@type":"WebSite","@id":"https:\/\/atmokpo.com\/w\/#website","url":"https:\/\/atmokpo.com\/w\/","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","description":"","publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/atmokpo.com\/w\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ko-KR"},{"@type":"Organization","@id":"https:\/\/atmokpo.com\/w\/#organization","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","url":"https:\/\/atmokpo.com\/w\/","logo":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/","url":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","contentUrl":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","width":400,"height":400,"caption":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8"},"image":{"@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/bebubo4"]},{"@type":"Person","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7","name":"root","image":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","caption":"root"},"sameAs":["http:\/\/atmokpo.com\/w"],"url":"https:\/\/atmokpo.com\/w\/author\/root\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/37511","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/comments?post=37511"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/37511\/revisions"}],"predecessor-version":[{"id":37512,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/37511\/revisions\/37512"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=37511"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=37511"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=37511"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}