Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Svg Image object binding. #255

Open
Alexbeerses opened this issue Dec 20, 2024 Discussed in #254 · 1 comment
Open

Svg Image object binding. #255

Alexbeerses opened this issue Dec 20, 2024 Discussed in #254 · 1 comment
Labels
bug Something isn't working

Comments

@Alexbeerses
Copy link

Alexbeerses commented Dec 20, 2024

Discussed in #254

Originally posted by Alexbeerses December 20, 2024
Good day!

I need to bind svg image object in avalonia xaml with Community MVVM.
I need to bind image object not image path because image comes from far source (dll) in my project.
The button pressing should make image changing in runtime. I tried to use SvgSource but it didn't work.
I have make a simple project to show the behavior.

AvaloniaSvg.zip

Could you help?

    <StackPanel
        HorizontalAlignment="Center"
        VerticalAlignment="Center"
        Orientation="Vertical">
        
        <Image Width="128">
            <Image.Source>
                <SvgImage Source="{Binding StateImage}" />
            </Image.Source>
        </Image>

        <Button HorizontalAlignment="Center" Command="{Binding StateImageChange}">Change</Button>

    </StackPanel>
public partial class MainWindowViewModel : ViewModelBase
{
    //-----------------------------------------------------------------------------------------------

    const string SvgPathPrefix = "avares://AvaloniaSvg/Assets/";

    //-----------------------------------------------------------------------------------------------

    [ObservableProperty]
    SvgSource? _StateImage = SvgImageGet("BallRed");

    //-----------------------------------------------------------------------------------------------

    [RelayCommand]
    public void StateImageChange()
    {
        StateImage = SvgImageGet("BallGreen");
    }
    //-----------------------------------------------------------------------------------------------

    public static SvgSource? SvgImageGet(string name)
    {
        var path = $"{SvgPathPrefix}{name}.svg";

        try
        {
            return SvgSource.Load(path);
        }
        catch (Exception ex)
        {
            string s = ex.Message;
        }
        return null;
    }
    //-----------------------------------------------------------------------------------------------
}
@KarenArzumanyan
Copy link

This problem creates a lot of inconvenience and difficulty.
In cases where an image is loaded from a separate library, using a path does not provide a solution.
Please help us fix it

@wieslawsoltes wieslawsoltes added the bug Something isn't working label Dec 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants