심각도 코드 설명 프로젝트 파일 줄 비표시 오류(Suppression) 상태 오류 NU1107 Xamarin.Android.Support.Compat에 대한 버전 충돌이 검색되었습니다. 프로젝트에서 패키지를 직접 참조하여 이 문제를 해결하세요. Project.Android -> Xamarin.Android.Support.v7.MediaRouter 220.127.116.11 -> Xamarin.Android.Support.v7.Palette 18.104.22.168 -> Xamarin.Android.Support.Compat (= 22.214.171.124) Project.Android -> MiGong -> Xamarin.Auth 126.96.36.199 -> Xamarin.Android.Support.CustomTabs 188.8.131.52 -> Xamarin.Android.Support.Compat (= 184.108.40.206). 오류 패키지 복원에 실패했습니다. 'MiGong.Android'에 대한 패키지 변경 내용을 롤백하고 있습니다. 0 오류 Xamarin.Android.Support.Compat에 대한 버전 충돌이 검색되었습니다. 프로젝트에서 패키지를 직접 참조하여 이 문제를 해결하세요. Project.Android -> Xamarin.Android.Support.v7.MediaRouter 220.127.116.11 -> Xamarin.Android.Support.v7.Palette 18.104.22.168 -> Xamarin.Android.Support.Compat (= 22.214.171.124) Project.Android -> Xamarin.Auth 1.6.0 -> Xamarin.Android.Support.CustomTabs 126.96.36.199 -> Xamarin.Android.Support.Compat (= 188.8.131.52).
Xamarin.Auth 를 사용하려는데 위와 같은 에러가 발생되었다.
찾아보니 해결방법은 역시 에러 메세지에 있었다.
Android 프로젝트의 Nuget 에서
Xamarin.Android.Support.Compat 을 찾아 설치하고
그다음으로 Xamarin.Android.Support.v7.Palette 을 설치하면된다.
(순서가 바뀌면 설치되지 않는다.)
그리고 다시 android 프로젝트의 Nuget 에서 Xamarin.Auth 를 설치하면 설치가 제대로 된다.
Fullscreen Video Background Control for Xamarin.Forms
How to play video as page background, like Spotify and Uber, using Xamarin.Forms with Android and iOS Custom Renderer.
One of the cool trend on mobile UI I've seen a lot is using video as View background. You can see it on some big mobile app products like Tumblr, Spotify, and Vine. As you can see, they have this cool Home View with sign in and sing up button with video playing in background. This feature is so cool and can make your app look more professional. This time, I'll show you how to implement it in Xamarin.Forms app. All we need is to implement two custom renderers for Android and iOS each.
Note: Please be aware on implementing this feature. It can make your app freeze, draining phone battery, or even make the phone lag. It shouldn't be a problem for the latest devices as long as the video file is in reasonable resolution and file size. Also see some guides on encoding the video into h.264, also mentioned in this tutorial.
Creating Video View Control for Xamarin.Forms
Let's create a new Xamarin.Forms PCL project first and name it BackgroundVideo. Now let's head to the PCL library and create a new class called Video inherited from Xamarin.Forms.View.
For the sake of the tutorial, we're going to make this control with simple requirements.
We need a bindable property to point which video to be displayed. I'm going to call it Source property. It's a string to locate which video file to be played. On iOS, Source property is relative to Resources directory as for Android, it is relative to Assets directory.
Next thing we need is a boolean to define if we want the video in loop or not. Let's call this property Loop. By default, I set this value as true so when you set a video Source property, it would be looped by default.
Finally, we're going to need a callback fired when video is finished. For simplicity, I use Action class called OnFinishedPlaying. You can modify it to event or anything you comfortable with.
After we created this class, next thing to do is to implement custom renderers for both iOS and Android.
Video View Control iOS Custom Renderer
First thing to do is to create a custom renderer class called VideoRenderer inherited from ViewRenderer<Video, UIView>. The idea is to use iOS native video player with the help of MPMoviePlayerController class and set its native control to our Video custom view. Also we're going to need an NSObject to listen the event from video player wether it is ended or not.
To start iOS video player, we need to check wether the video from Source property exists in Resources budnle or not. If it doesn't exist, we'll display an empty view.
If the video file exists, we need to create MPMoviePlayerController and parse the location of the video file as NSUrl. To make our custom control clear, without border or anything, we need to set ControlStyle to MPMovieControlStyle.None and background color to UIColor.Clear.
Also, we probably will have one video file for any resolution. You don't want it to look stretched on some device, right? To make the video resolution looks consistent, we need to set video player ScalingMode to MPMovieScalingMode.AspectFill.
We also have this Loop property to define wether the video playing will be looped or not. To set it to loop, we need to change video player RepeatMode to MPMovieRepeatMode.One. Otherwise, set it to MPMovieRepeatMode.None.
Finally, to make video player play the file, we call PrepareToPlay() function. To display the video to our custom control, we need to use SetNativeControl() function.
The rest of the code is to override OnElementChanged and OnElementPropertyChanged function so it can be functionally working from Xamarin.Forms project. Under OnElementChanged, we need to listen to video player playback finish event and invoke OnFinishedPlaying action. The following snippet is the simplest code necessary to make it work.
Now that iOS implementation is completed, let's head to our Android project.
Video View Custom Renderer for Android
Create a new custom renderer on Android project and let's name it VideoRenderer, too. We'll inherit this renderer with ViewRenderer<Video, FrameLayout>, meaning it will be displayed as FrameLayout in native Android control.
One thing that made Android implementation a bit complicated is that we need two kind of views if you want to cover old Android versions. If you just want to cover modern Android OS from Ice Cream Sandwich or more, you can just focus on TextureView implementation, if not you'll also need to implement it using VideoView.
Please note that VideoView implementation here is not optimal. Maybe you'll notice some flickering. That's why I add view called _placeholder. This is just an empty view. It'll be displayed when no video playing or when in video source changed transition. If the video file ready to play and display, _placeholder will be hidden.
Now before we thing about what video container to use, we need to implement the video player itself. Android already provide us with their MediaPlayer class. We'll need to use this object and make sure it only created once. We can reuse the same object if we change the video source.
We need to set Completion event to implement our OnFinishedPlaying callback. We also need to set Looping property to our custom Loop property.
There is one thing that different from our iOS implementation, there is no easy property set to display video resolution as aspect fill! That means we need to implement our own method into custom function called AdjustTextureViewAspect(). This function will be called on VideoSizeChanged callback. We'll talk about this implementation later.
Now that we have our video player object, next thing is to create function that play video from Source property. Please remember that video file on Android need to be stored under Assets directory. We can open this file by using Assets.OpenFd(fullPath) function.
If the file doesn't exist, it'll throw Java.IO.IOException. That means we don't need to display anything on our video container.
If the file exists, we just need to reset our video player, then set data source based on previous step. We can't just play the video directly, so we need to prepare it first. When preparation complete, it'll trigger Prepared event and display the video to one of our implemented video view from previous step.
As previously mentioned, Android doesn't provide us easy property to scale our video to aspect fill. You know it yourself that Android devices have so many screen resolution so keep the video like it is is not an option. We need to scale it properly so it won't look stretched.
Good news is, we can do that if we use TextureView. Bad news is for now I don't know how to implement it with VideoView. But it's better than nothing right?
The idea to make video scale properly is to use matrix to scale the content of TextureView. It is scaled up or down based on video size and view size. Then, after it's scaled, it is positioned at the center of the view.
As mentioned earlier, if we want to support a wide range of Android OS, we need to implement it into TextureView and VideoView. This will be implemented under OnElementChanged function. Both implementation have some same properties. We will make their Background color to transparent and layout parameters to match parent. This way it won't have any color to display when there is no video, and it'll fill entire container.
Following snippet is how to implement it on our Video custom renderer. You see it's similar with our iOS implementation, except for container creation and video playing.
Since we're using TextureView and VideoView, there is some function from interfaces need to be implemented. One of them is to remove video when texture or surface is destroyed. To do that, we're going to need to set >_placeholder visibility to visible.
When using TextureView, we need to implement TextureView.ISurfaceTextureListener interface. We set video player's surface when texture available and hide it when texture destroyed. Following snippet shows you how to implement it.
When using VideoView, we need to implement ISurfaceHolderCallback interface. Similar with TextureView, we set video player's display when surface created and hide it when surface destroyed. The complete implementation of this interface can be see on following snippet.
That's all we need for Android. Now that we all have everything needed, we can test this control to Xamarin.Forms Page.
Testing to Xamarin.Forms Page
Before we create a test page, I recommend you to prepare your own video file. It is recommended as vertical video so a lot of space won't be wasted.
But, if you don't have any video to test, don't worry. You can download free videos to use from Coverr. They don't have any vertical videos, we can still use it. You can either crop it into vertical video or you can just use it as it is since we already handle scaling to aspect fill on our code.
So use any video you like. I recommend any file as long as it's mp4 video with h264 encoding. In this tutorial, I use video from Coverr called Orchestra. You can download it from here.
Note: For some Android and iOS devices, especially the old products, they probably can't play some mp4 files. This is mostly caused by not-supported baseline profile. To fix that, you can re-encode the video using a tool like ffmpeg and change its baseline profile based on your preferences. See following table to check baseline profile compatibility with iOS. See Supported Media Formats from official Android guide, too.
-profile:v baseline -level 3.0
iPhone 3G and later, iPod touch 2nd generation and later
-profile:v baseline -level 3.1
iPad (all versions), Apple TV 2 and later, iPhone 4 and later
-profile:v main -level 3.1
Apple TV 3 and later, iPad 2 and later, iPhone 4s and later
-profile:v main -level 4.0
Apple TV 3 and later, iPad 2 and later, iPhone 4s and later
-profile:v high -level 4.0
iPad 2 and later, iPhone 4s and later, iPhone 5c and later
After you get your video file, place it to the folders for each OS. On Android, you should put it under Assets directory. On iOS, you should put it under Resources directory. For this tutorial I put the file under Assets/Videos on Android and Resources/Videos on iOS.
Once you put them all to correct folder, we need to create our Page on Xamarin.Forms PCL project.
This is a simple page with smallest components. We'll create a Home Page, with video background, two text boxes for username and password, and to buttons for sign in and sign up. There is no logic in this page, I just want to show you how to make a beautiful home page.
For better controls placement, I use Grid as container. See following snippet for the complete XAML.
That's it. If you don't want the video to be looped, just change its Loop property. If you want to do something when video ended, just set OnFinishedPlaying from C# code. Now let's see how it runs.
See it in Action
If you set everything correctly, The following figure is how it run on iOS device or emulator. As you can see, there are two text boxes and two buttons. The video is playing as the page background smoothly.
Similar with iOS version, the following animated gif image shows how it looks on Android device or emulator. See that text box style difference from iOS version. But let's care about it later, the point is video background consistently work just like iOS.
All you need to do the rest is to make styling more consistent through any platforms.
Once again, all I can say is you can make any cross platform control you want by using Custom Renderer. As long as you understand how to code in native language (well, you can Google it though), you can create anything.
As for performance, I believe I said it earlier, you probably see some flickering on old Android devices. For now I don't have any idea to optimize it.
If you have any idea and suggestion, feel free to leave a comment below.
Sample Xamarin.Forms 3.0 Phone App showed in Microsoft Build 2018. Use your camera during a conference to capture your experience. Let Vision do the heavy lifting of identifying known products and scan your images to text for easier searching.
BikeSharing360 is a fictitious example of a smart bike sharing system with 10,000 bikes distributed in 650 stations located throughout New York City and Seattle. Their vision is to provide a modern and personalized experience to riders and to run their business with intelligence. Was a demo in the Connect(); 2016.
This app is around 15,000 lines of code. The iOS version contains 93% shared code, the Android version contains 90% shared code, the UWP has 99% shared code, and our Azure backend contains 23% shared code with the clients!
Android, iOS & UWP.
Azure + Online/Offline Sync
Maps & Navigation
URL Navigation (Universal Links + Google App Indexing)
Async programming is all the rage in mobile app development for good reasons. Using async methods for long running tasks, like downloading data, helps keep your user interface responsive, while not using async methods, or the improper use of async/await, can cause your app’s UI to stop responding to user input until the long running task completes. This can result in a poor user experience, which can then lead to poor reviews on the app stores, which is never good for business.
Today we’ll take a look at the use of async and how to utilize it to prevent jerky and unexpected behaviors in a ListView.
What is async/await?
The async and await keywords were introduced in .NET 4.5 to make calling async methods easier and to make your async code more easily readable. The async/await API is syntactic sugar that uses the TPL (Task Parallel Library) behind the scenes. If you wanted to start a new task and have code run on the UI thread after the task completes prior .NET 4.5, your code would have looked something like this:
// Start a new task (this launches a new thread)
// Do some work on a background thread, allowing the UI to remain responsive
// When the background work is done, continue with this code block
// the following forces the code in the ContinueWith block to be run on the
That’s not very pretty. Using async/await, the above becomes:
The above code gets compiled behind the scenes to the same TPL code as it does in the first example, so as noted, this is just syntactic sugar, and how sweet it is!
Using Async: Pitfalls
In reading about using async/await, you may have seen the phrase “async all the way” thrown around, but what does that really mean? Simply put, it means that any method that calls an async method (i.e. a method that has the async keyword in its signature) should use the await keyword when calling the async method. Not using the await keyword when calling an async method can result in exceptions that are thrown being swallowed by the runtime, which can cause issues that are difficult to track down. Using the await keyword requires that the calling method also use the async keyword in its signature. For example:
This poses a problem if you want to call an async method using the await keyword when you can’t use the async modifier on the calling method, for instance if the calling method is a method whose signature can’t use the async keyword or is a constructor or a method that the OS calls, such as GetView in an Android ArrayAdapter or GetCell in an iOS UITableViewDataSource. For example:
// Can’t use await keyword in this method as you can’t use async keyword
// in method signature due to incompatible return type.
As you may know, an async method has to return either void, Task, or Task<T>, and returning void should only be used when making an event handler async. In the case of the GetView method noted above, you need to return an Android View, which can’t be changed to return Task<View> as the OS method that calls it obviously does not use the await keyword and so can’t handle a Task<T> being returned. Thus you can’t add the async keyword to the above method and therefore can’t use the await keyword when calling an async method from the above method.
To get around this, one might be tempted, as I have been in the past, to just call a method from GetView (or similar method where the signature can’t be changed regardless of the platform) as an intermediate method, and then call the async method from the intermediate method:
The problem here is that IntermediateMethod is now an async method and thus should be awaited just like the MyMethodAsync method needed to be. So, you have gained nothing here, as IntermediateMethod is now async and should be awaited. In addition, the GetView method will continue running all of the code after calling IntermediateMethod(), which may or may not be desirable. If the code following the call to IntermediateMethod() depends on the results of the IntermediateMethod(), then it isn’t desirable. In such a scenario, you may be tempted to use the Wait() method call (or Result property) on the async task, e.g.:
Calling Wait() on the async method causes the calling thread to pause until the async method completes. If this is the UI thread, as would be the case here, then your UI will hang while the async task runs. This isn’t good, especially in an ArrayAdapter that is supplying the data for the rows of a ListView. The user will not be able to interact with the list view until the data for all of the rows has been downloaded, and scrolling will likely be jerky and/or completely non-responsive, which isn’t a good user experience. There’s also a Result property you can call on the async task. This would be used if your async task was returning data by using Task<T> as the return type of the async method. This would also cause the calling thread to wait for the result of the async task:
returnawaitMyMethodAsync();// MyMethodAsync also returns Task<string> in this example
In fact doing the above may cause your UI to hang completely and for the ListView never to be populated, which is a non-starter. It may also just be jerky:
In general, you should avoid using Wait() and Result, especially on the UI thread. In the iOS and Android sample projects linked at the end of this blog, you can look in ViewControllerJerky and MainActivityJerky respectively to see this behavior. Those files are not set to compile in the sample projects.
Using Async All the Way
So how do I get “async all the way” in this scenario?
One way around the above problems is to revert to the old TPL upon which async/await is based. You’re going to use TPL directly, but only once to start the chain of async method calls (and to start a new thread right away). Somewhere down the line the TPL will be used directly again, as you need to use TPL to start a new thread. You can’t start a new thread using only the async/await keywords, so some method down the chain will have to launch the new thread with TPL (or another mechanism). The async method that launches a new thread will be a framework method, like a .NET HttpClient async method in many, if not most, cases. If not using async framework methods, then some method of yours down the chain will have to launch a new thread and return Task or Task<T>.
Let’s start with an example using GetView in an Android project (though the same concept will work for any platform, i.e. Xamarin.iOS, Xamarin.Forms, etc.) Let’s say I have a ListView that I want to populate with text downloaded from the web dynamically (more likely one would download the whole list of strings first and then populate the list rows with the already downloaded content, but I’m downloading the strings row by row here for demonstration purposes, plus there are occasions where one may want to do it this way anyway). I certainly don’t want to block the UI thread waiting for the multiple downloads; rather, I would like the user to be able to start working with the ListView, scroll around, and have the text appear in each ListView cell as the text gets downloaded. I also want to make sure that if a cell scrolls out of view, that when it is reused it will cancel loading the text that is in the process of being downloaded and start loading new text for that row instead. We do this with TPL and cancellation tokens. Comments in the code should explain what’s being done.
In a nutshell, the above method checks to see if this is a reused cell and, if so, we cancel the existing async text download if still incomplete. It then loads placeholder text into the cell, launches the async task to download the correct text for the row, and returns the view with placeholder text right away, thereby populating the ListView. This keeps the UI responsive and shows something in the cell while the launched task does its work of getting the correct text from the web. As the text gets downloaded, you’ll see the placeholders change to the downloaded text one-by-one (not necessarily in order due to differing download times). I added a random delay to the async task to simulate this behavior since I’m making such a simple, quick request.
Note that I can decorate the lambda passed into Task.Run() with the async keyword, thus allowing me to await the call to my async method, and thereby achieving “async all the way.” No more Jerky ListView!
See it in action
If you want to see the above in action for Xamarin.iOS, Xamarin.Android, and Xamarin.Forms, check it out on my GitHub repo. The iOS version is very similar to the above, the only difference being in how I attach the CancellationTokenSource to the cell since there is no Tag property as there is in an Android View. Xamarin.Forms, however, does not have a direct equivalent to GetView or GetCell that I’m aware of, so I simulate the same behavior by launching an async task from the main App class constructor to get the text for each row.