admin管理员组

文章数量:1406140

I am loading a pop up page having below slider:

<Slider 
    x:Name="audioSlider" 
    Grid.Row="0"
    Minimum="0" 
    VerticalOptions="Center"
    HorizontalOptions="Fill" 
    ThumbColor="Transparent" 
    MaximumTrackColor="Gray" 
    MinimumTrackColor="White" 
    InputTransparent="True"
    Maximum="{Binding Source={x:Reference MymediaElement}, Path=Duration.TotalSeconds}"
    Value="{Binding Source={x:Reference MymediaElement}, Path=Position.TotalSeconds, Mode=OneWay}"> 
    <Slider.WidthRequest>
        <OnIdiom x:TypeArguments="x:Double">
            <OnIdiom.Phone>400</OnIdiom.Phone>
            <OnIdiom.Tablet>980</OnIdiom.Tablet>
            <OnIdiom.Desktop>400</OnIdiom.Desktop>
        </OnIdiom>
    </Slider.WidthRequest>
</Slider>

My problem is this Pop up page is loading on LIVE running and not loading when normally opens the app. I am playing an audio on this page and When plays the audio I am using this Slider to show the progress. I am using MediaElement for playing audio and I am adding it from the code behind like below:

public static MediaElement MymediaElement;
public DRAudioPopUpPage(string audioUrl, string date)
{
    try
    {
        MymediaElement = new MediaElement();
        MymediaElement.WidthRequest = 1;
        MymediaElement.HeightRequest = 1;
        MymediaElement.ShouldAutoPlay = false;
        MymediaElement.Source = audioUrl;
        myGrid.Add(MymediaElement);
    }
    catch (Exception ex)
    {
        System.Diagnostics.Debug.WriteLine("DRAudioaudiourl_color_Exception:>" + ex);
    }
    
    // Initialize Timer
    timer = Application.Current.Dispatcher.CreateTimer();
    timer.Interval = TimeSpan.FromMilliseconds(500);
    timer.Tick += (s, e) => UpdateSlider();
}

If I remove the Maximum and Value properties from slider this issue is not getting.

Maximum="{Binding Source={x:Reference MymediaElement}, Path=Duration.TotalSeconds}"
Value="{Binding Source={x:Reference MymediaElement}, Path=Position.TotalSeconds, Mode=OneWay}"> 

So I tried adding fallback values for both like below but no luck.

Maximum="{Binding Source={x:Reference MymediaElement}, Path=Duration.TotalSeconds, FallbackValue=100}"
Value="{Binding Source={x:Reference MymediaElement}, Path=Position.TotalSeconds, Mode=OneWay, FallbackValue=0}"> 

Update

I am opening the pop up page from another contentpage when taps on an image. Below code I am using to open the pop up page.

public async void PlayAudio(object sender, EventArgs args)
{
    try
    {
        if (!string.IsNullOrWhiteSpace(mAudioURL))
        {
            await Application.Current.MainPage.ShowPopupAsync(new DRAudioPopUpPage(mAudioURL, newDate));
        }
        else
        {
            Utility.ShowToast("Audio is not available! please try another day!", ToastDuration.Short, 14);
        }
    }
    catch (Exception ex)
    {
        System.Diagnostics.Debug.WriteLine("Exception:>" + ex);
    }
}

I have created a sample demo and uploaded here.

I am loading a pop up page having below slider:

<Slider 
    x:Name="audioSlider" 
    Grid.Row="0"
    Minimum="0" 
    VerticalOptions="Center"
    HorizontalOptions="Fill" 
    ThumbColor="Transparent" 
    MaximumTrackColor="Gray" 
    MinimumTrackColor="White" 
    InputTransparent="True"
    Maximum="{Binding Source={x:Reference MymediaElement}, Path=Duration.TotalSeconds}"
    Value="{Binding Source={x:Reference MymediaElement}, Path=Position.TotalSeconds, Mode=OneWay}"> 
    <Slider.WidthRequest>
        <OnIdiom x:TypeArguments="x:Double">
            <OnIdiom.Phone>400</OnIdiom.Phone>
            <OnIdiom.Tablet>980</OnIdiom.Tablet>
            <OnIdiom.Desktop>400</OnIdiom.Desktop>
        </OnIdiom>
    </Slider.WidthRequest>
</Slider>

My problem is this Pop up page is loading on LIVE running and not loading when normally opens the app. I am playing an audio on this page and When plays the audio I am using this Slider to show the progress. I am using MediaElement for playing audio and I am adding it from the code behind like below:

public static MediaElement MymediaElement;
public DRAudioPopUpPage(string audioUrl, string date)
{
    try
    {
        MymediaElement = new MediaElement();
        MymediaElement.WidthRequest = 1;
        MymediaElement.HeightRequest = 1;
        MymediaElement.ShouldAutoPlay = false;
        MymediaElement.Source = audioUrl;
        myGrid.Add(MymediaElement);
    }
    catch (Exception ex)
    {
        System.Diagnostics.Debug.WriteLine("DRAudioaudiourl_color_Exception:>" + ex);
    }
    
    // Initialize Timer
    timer = Application.Current.Dispatcher.CreateTimer();
    timer.Interval = TimeSpan.FromMilliseconds(500);
    timer.Tick += (s, e) => UpdateSlider();
}

If I remove the Maximum and Value properties from slider this issue is not getting.

Maximum="{Binding Source={x:Reference MymediaElement}, Path=Duration.TotalSeconds}"
Value="{Binding Source={x:Reference MymediaElement}, Path=Position.TotalSeconds, Mode=OneWay}"> 

So I tried adding fallback values for both like below but no luck.

Maximum="{Binding Source={x:Reference MymediaElement}, Path=Duration.TotalSeconds, FallbackValue=100}"
Value="{Binding Source={x:Reference MymediaElement}, Path=Position.TotalSeconds, Mode=OneWay, FallbackValue=0}"> 

Update

I am opening the pop up page from another contentpage when taps on an image. Below code I am using to open the pop up page.

public async void PlayAudio(object sender, EventArgs args)
{
    try
    {
        if (!string.IsNullOrWhiteSpace(mAudioURL))
        {
            await Application.Current.MainPage.ShowPopupAsync(new DRAudioPopUpPage(mAudioURL, newDate));
        }
        else
        {
            Utility.ShowToast("Audio is not available! please try another day!", ToastDuration.Short, 14);
        }
    }
    catch (Exception ex)
    {
        System.Diagnostics.Debug.WriteLine("Exception:>" + ex);
    }
}

I have created a sample demo and uploaded here.

Share Improve this question edited Mar 10 at 5:51 Matthew Pans asked Mar 6 at 11:37 Matthew PansMatthew Pans 8751 gold badge11 silver badges29 bronze badges 8
  • Did you mean the binding for the slider not worked when it was in the popup? – Liyun Zhang - MSFT Commented Mar 7 at 1:20
  • @LiyunZhang-MSFT Everything is working in LIVE running and able to view the pop up window. When normally tries to open pop up window is not showing. – Matthew Pans Commented Mar 7 at 7:09
  • Did you mean there is no problem in the debug mode but the pop up window not show when user clicks the appicon to run the application? – Liyun Zhang - MSFT Commented Mar 7 at 8:18
  • @LiyunZhang-MSFT Yes, when launching app by appicon normally the pop up window is not showing. If I run the app via Visual Studio, at that time pop up window is showing. – Matthew Pans Commented Mar 7 at 8:56
  • @LiyunZhang-MSFT I run the app in release mode also, there also same issue, pop up page is not loading. – Matthew Pans Commented Mar 7 at 9:43
 |  Show 3 more comments

1 Answer 1

Reset to default 0

I cloned your project and reproduced your problem. And when I run the project in the release mode, it crushed and the logs show the following error:

[Microsoft.Maui.Controls.Xaml.XamlParseException]: Position 51:17. Can not find the object referenced by `MymediaElement`

And I checked the DRAudioPopUpPage.xaml, there is no <MediaElement x:Name="MymediaElement" .../> in it. So the app crashed.

To fix it, you can bind the Slider to the correct MediaElement. And I see you declared public static MediaElement MymediaElement; in the DRAudioPopUpPage. So you can bind it to the static MediaElement such as:

  xmlns:local="clr-namespace:CatholicBrain.Daily_Activities"  
  <!--- the namespace of the DRAudioPopUpPage -->  
....  
....  
      Maximum="{Binding Source={x:Static local:DRAudioPopUpPage.MyMediaElement}, Path=Duration.TotalSeconds}"  
      Value="{Binding Source={x:Static local:DRAudioPopUpPage.MyMediaElement}, Path=Position.TotalSeconds, Mode=OneWay}">

For the demo you provided:

Unfortunately it is showing handler not found exception, but there is no handlers I am using on the pages

Adding following code in the MauiProgram.cs can fix it.

var builder = MauiApp.CreateBuilder();
builder
    .UseMauiApp<App>()
    .UseMauiCommunityToolkitMediaElement()

And then add the following code in the DRAudioPopUpPage.cs:

public static MediaElement MymediaElement;
public DRAudioPopUpPage(string audioUrl, string date)
{
        MymediaElement = new MediaElement();
        InitializeComponent()
}

After this, using the binding code above in the xaml can make the popup show in release mode.

本文标签: MAUI Pop up page is loading on LIVE running and not loading when normally opens the appStack Overflow